Skip to main content
The 2025 Developer Survey results are in. Explore insights into technology and tools, careers, community and more. View results.
314 votes
10 answers
189k views

Type converting slices of interfaces

I'm curious why Go does't implicitly convert []T to []interface{} when it will implicitly convert T to interface{}. Is there something non-trivial about this conversion that I'm missing? Example: ...
danny's user avatar
  • 10.5k
617 votes
4 answers
191k views

What are the use(s) for struct tags in Go?

In the Go Language Specification, it mentions a brief overview of tags: A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the ...
liamzebedee's user avatar
  • 14.6k
184 votes
1 answer
56k views

json.Marshal(struct) returns "{}"

type TestObject struct { kind string `json:"kind"` id string `json:"id, omitempty"` name string `json:"name"` email string `json:"email"` } func TestCreateSingleItemResponse(t *...
Doug Knesek's user avatar
  • 6,717
548 votes
5 answers
168k views

Pointers vs. values in parameters and return values

In Go there are various ways to return a struct value or slice thereof. For individual ones I've seen: type MyStruct struct { Val int } func myfunc() MyStruct { return MyStruct{Val: 1} } ...
Zef Hemel's user avatar
  • 6,747
100 votes
2 answers
64k views

JSON and dealing with unexported fields

Is there a technical reason why unexported fields are not included by encoding/json? If not and it is an arbitrary decision could there be an additional back door option (say '+') to include even ...
user1338952's user avatar
  • 3,431
34 votes
3 answers
6k views

No output from goroutine

While SayHello() executes as expected, the goroutine prints nothing. package main import "fmt" func SayHello() { for i := 0; i < 10 ; i++ { fmt.Print(i, " ") } } func main() { ...
Dinesh Panchananam's user avatar
70 votes
3 answers
40k views

Hiding nil values, understanding why Go fails here

I fail to understand how to correctly assure that something is not nil in this case: package main type shower interface { getWater() []shower } type display struct { SubDisplay *display } func (...
sharpner's user avatar
  • 3,977
330 votes
20 answers
385k views

What's the proper way to "go get" a private repository?

I'm searching for the way to get $ go get work with private repository, after many google try. The first try: $ go get -v gitlab.com/secmask/awserver-go Fetching https://gitlab.com/secmask/...
secmask's user avatar
  • 8,187
259 votes
8 answers
411k views

Parsing RFC-3339 / ISO-8601 date-time string in Go

I tried parsing the date string "2014-09-12T11:45:26.371Z" in Go. This time format is defined as: RFC-3339 date-time ISO-8601 date-time Code layout := "2014-09-12T11:45:26.371Z" ...
kannanrbk's user avatar
  • 7,144
139 votes
7 answers
134k views

Multiple values in single-value context

Due to error handling in Go, I often end up with multiple values functions. So far, the way I have managed this has been very messy and I am looking for best practices to write cleaner code. Let's ...
Spearfisher's user avatar
  • 8,843
435 votes
4 answers
176k views

X does not implement Y (... method has a pointer receiver)

There are already several Q&As on this "X does not implement Y (... method has a pointer receiver)" thing, but to me, they seems to be talking about different things, and not applying to my ...
xpt's user avatar
  • 23.4k
190 votes
8 answers
89k views

What's the meaning of interface{}?

I'm new to interfaces and trying to do SOAP request by github I don't understand the meaning of Msg interface{} in this code: type Envelope struct { Body `xml:"soap:"` } type Body struct {...
user's user avatar
  • 2,902
151 votes
5 answers
108k views

Are slices passed by value?

In Go, I am trying to make a scramble slice function for my traveling salesman problem. While doing this I noticed when I started editing the slice I gave the scramble function was different every ...
duck's user avatar
  • 1,924
211 votes
5 answers
165k views

How do I do a literal *int64 in Go?

I have a struct type with a *int64 field. type SomeType struct { SomeField *int64 } At some point in my code, I want to declare a literal of this (say, when I know said value should be 0, or ...
ThisGuy's user avatar
  • 2,883
119 votes
2 answers
26k views

What does an underscore and interface name after keyword var mean?

From http://golang.org/src/pkg/database/sql/driver/types.go: type ValueConverter interface { // ConvertValue converts a value to a driver Value. ConvertValue(v interface{}) (Value, error) } ...
dilfish's user avatar
  • 1,429

15 30 50 per page
1
2 3 4 5
502