This question already has an answer here:
Why there are two ways of declaring variables in Go, what's the difference and which to use?
(1 answer)
Closed 7 months ago.
var client := http.Client
For whatever reason this code is giving the error message missing variable or initialization. Can someone enlighten me on why? I'm not understanding what I have done wrong.
In Go we use := or var = for initializing variables. In your case you can re-write it to be:
var client = http.Client{}
or
client := http.Client{}
Either of these will trigger type inference of the variable. You can use var with a type to explicitly declare a type as well. In your case if you wanted to enforce the type you could write:
var client http.Client = http.Client{}
Related
I found the following code
client := &http.Client
What does & mean? What kind of value that client var receives
& is the "pointer to" operator, similar to c. The client variable holds a pointer to the value of http.Client.
client here is a pointer to the instance of http.Client.
It is same as:
var client *http.Client
client = &http.Client{...}
This question already has answers here:
Multiple values in single-value context
(6 answers)
Closed 6 years ago.
In Go I can use the underscore to ignore a return value from a function that returns multiple values. For instance:
res, _ := strconv.Atoi("64")
Suppose I wanted to use the first value directly into another function call (while ignoring error checking best practices, in this example):
myArray := make([]int, strconv.Atoi("64"))
The compiler will complain that I'm using a multiple-value function in a singe-value context:
./array-test.go:11: multiple-value strconv.Atoi() in single-value context
Is it possible to "pick and choose" from the return values in a single-line without resorting to auxiliary functions?
The only real way to do it is to create some utility "bypass" function, and since this is Go, you'll have to declare one per type.
for example:
func noerrInt(i int, e err) int {
return i
}
then you can do:
myArray := make([]int, noerrInt(strconv.Atoi("64")))
But really, this pretty much sucks, AND ignores best practices.
This question already has answers here:
Multiple values in single-value context
(6 answers)
Closed 6 years ago.
I'm trying to initialize a struct in Go, one of my values is being which returns both an int and an error if one was encountered converted from a string using strconv.Atoi("val").
My question is : Is there a way to ignore an error return value in Golang?
ts := &student{
name: td[0],
ssn: td[2],
next: students.next,
age: strconv.Atoi(td[1]),
}
which gives the error
multiple-value strconv.Atoi() in single-value context
if I add in the err, which i don't want to include in my struct, I will get an error that I am using a method that is not defined in the struct.
You can ignore a return value using _ on the left hand side of assignment however, I don't think there is any way to do it while using the 'composite literal' initialization style you have in your example.
IE I can do returnValue1, _ := SomeFuncThatReturnsAresultAndAnError() but if you tried that in your example like;
ts := &student{
name: td[0],
ssn: td[2],
next: students.next,
age, _: strconv.Atoi(td[1]),
}
It will also generate a compiler error.
This question already has answers here:
What is this "err.(*exec.ExitError)" thing in Go code? [duplicate]
(2 answers)
Closed 7 years ago.
I am reading this code and I don't quite understand what line #2 does:
resp := route.Handler(req)
_, nilresponse := resp.(NilResponse)
if !nilresponse {
type NilResponse struct {
}
Thank you
This isn't an empty function name. This is a type-assertion. It is testing that resp is a NilResponse. If it is, then nilResponse will be true, otherwise it will be false. This code throws away the resulting type-asserted value by using _.
See Type Assertions.
If line two is _, nilresponse := resp.(NilResponse) then it's not a function call at all. It's a type assertion. The code is saying "the interface value represented by resp is of type NilResponse.
EDIT; your assignment is kind of odd though because the first return value would be the NilResponse object and the second (if specified) is a flag to indicate whether or not it worked (or maybe an error, can't remember if it's a bool or error). So typically it would be something like; nilResponse, ok := or nilResponse, err :=
This question already has answers here:
What does an underscore and interface name after keyword var mean?
(2 answers)
Closed 2 years ago.
When I read a copy of the docker/distribution source code, I find there are variables declared which make me quite confused.
The code is:
var _ FileInfo = FileInfoInternal{}
var _ FileInfo = &FileInfoInternal{}
I don't know what the declare mean, and hope to get some help.
From the FAQ:
You can ask the compiler to check that the type T implements the
interface I by attempting an assignment:
type T struct{}
var _ I = T{} // Verify that T implements I.
In this case the blank identifier _ stands for the variable name which is not needed here (and thus prevents a "declared but not used" error).
And more general from the spec:
The blank identifier provides a way to ignore right-hand side values
in an assignment:
_ = x // evaluate x but ignore it
x, _ = f() // evaluate f() but ignore second result value
By testing both FileInfoInternal{} and &FileInfoInternal{} you check if the interface is implemented with a value receiver. A value receiver will accept both a value and a pointer whereas the pointer receiver will only work with a pointer and the first assignment by value will fail.
The second test with &FileInfoInternal{} is not actually needed (as confirmed by the author in the comments) since the first test will pass with a value receiver and fail with a pointer received. Thus the second test is redundant.
This is an excellent article that explains the difference between value and pointer receivers and how they are used very well.
FileInfo is an interface and the code checks whether FileInfoInternal implements this interface.