7,516 questions
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:
...
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 ...
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 *...
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}
}
...
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 ...
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() {
...
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 (...
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/...
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"
...
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 ...
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 ...
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 {...
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 ...
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 ...
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)
}
...