passing array or slice into variable args function in golang [duplicate] - go

This question already has answers here:
How can I pass a slice as a variadic input?
(3 answers)
Closed 7 years ago.
filepath.Join method takes in a ...string argument but I have a []string that I would like pass in. When I attempt to do this I get the following error:
cannot use append(elems, spadePath) (type []string) as type string in argument to filepath.Join
Is there a way to convert between a []type and a ...type?

Found a way to do this by appending the ... to your slice when being passed in as an argument.
For example, I was originally trying to call make the following call which was yielding the error:
filepath.Join(append(elems, basePath))
but I corrected it by appending ... in the argument:
filepath.Join(append(elems, basePath)...)

Related

What does a pair of round brackets syntax expression mean in Go? [duplicate]

This question already has answers here:
Typecasting in Golang
(5 answers)
Closed 2 years ago.
Reading this https://github.com/go-pg/pg/wiki/Writing-Queries#select I see many times this expression:
(*Book)(nil)
Example:
count, err := db.Model((*Book)(nil)).Count()
What does it mean?
That is a type conversion. Assuming the db.Model function takes interface{}, it sends an nil interface of type *Book to the function.
To convert a value v to type Book, you'd write:
Book(v)
However, you cannot write Book(nil) because nil is a pointer and Book is not. If you had a type
type BookPtr *Book
Then you could've written BookPtr(nil). Extending that, you want to write *Book(nil), but that means *(Book(nil)) which is invalid, hence:
(*Book)(nil)
'nil' is to Go what NULL/null is to other languages like C#/Java, etc. The *Variable is just getting the pointer value for the Book object of Model.
So in this case, I believe what's happening here is that (*Book)(nil) is setting the pointer value of the Book object of the Model to nil(/null).
Hope this helps in some way. 😊
Good Resource: https://go101.org/article/nil.html

Initialize gocql ips using a constant [duplicate]

This question already has answers here:
How can I pass a slice as a variadic input?
(3 answers)
Closed 4 years ago.
I need to initialize gocql with multiple ips, I want to pass the ips from a variable/constant.
How to pass some thing like
gocql.NewCluster(ipvalues)
instead of using
gocql.NewCluster("127.0.0.1", "127.0.0.2")
i want to pass the list of ips through a variable something like an array.
As you can see, gocql.NewCluser takes a variadic parameter, which means you can pass multiple values separated with commas to the function.
In go, you just need to make your ipvalues variable be a slice of strings and pass it like this:
ipvalues := []string{"127.0.0.1", "127.0.0.2"}
gocql.NewCluster(ipvalues...)
This will have the same effect as writing gocql.NewCluster("127.0.0.1", "127.0.0.2")
See the golang spec for more information on this feature

How can I use the result of a multi-value returning function as the argument of another function in Golang if I don't need error handling? [duplicate]

This question already has answers here:
Multiple values in single-value context
(6 answers)
Closed 4 years ago.
The following is what I want to achieve
fmt.Println(string(ioutil.ReadAll(res.Body)))
But this throws the following error.
multiple-value ioutil.ReadAll() in single-value context
I know that ioutil.ReadAll() returns the bytes and the error. But I don't want to write an extra line as follows
bytes, _ := ioutil.ReadAll(resp.Body)
Is it possible to just pass the result of ioutil.ReadAll() to fmt.Println() if don't care abut error handling in Go?
If you want it only once, it makes little sense in my opinion. However if this is a common situation and you feel that it improves code readability a lot, try something like:
// perhaps, there's a better name for this
func resultToString(b []byte, _ error) string {
return string(b)
}
// and later:
fmt.Println(resultToString(ioutil.ReadAll(res.Body)))
Unfortunately, that's a string-specific function, so for any other type you'll need to duplicate it.

Assign multi-value to struct literal [duplicate]

This question already has answers here:
Multiple values in single-value context
(6 answers)
Closed 7 years ago.
Is there any way in Go to do this:
segment := Segment{
CumulativeDistanceMm: strconv.Atoi(record[9]),
Length: strconv.Atoi(record[1]),
LinkId: strconv.Atoi(record[8]),
SegmentId: strconv.Atoi(record[2]),
}
The error that I get is that strconv.Atoi returns multiple values so I can't assign it directly to the struct properties. If it was a variable I could use the underscore to ignore the second value. Can I do something similar for structs?
strconv.Atoi can fail and you have to deal with this failure. If such failures are absolutely impossible you would write a function func MustAtoi(s string) int which panics on failure and use that one in your struct initialization.
In Go doing some programming instead of using syntactical sugar or fancy syntax is common.
Most probably you should rethink your error handling.

Swift: what's a use case for passing a primitive by reference [duplicate]

This question already has an answer here:
Does Swift have something like "ref" keyword that forces parameter to be passed by reference?
(1 answer)
Closed 8 years ago.
Swift has the inout keyword to pass a primitive argument by reference. When would I use this over just passing it by value?
Edit: I realize that you can use this to change its value, but why not just pass it by value and assign it the corresponding value in the tuple returned by the function?
You would do that if you wanted to modify the original value instead of just a copy. However, I would argue that you should just return the new value since you can return multiple values in Swift.
This seems to be a plausible reason:
"Maybe the existing body of Objective C libraries have a lot of out parameters, and they didn't want to wrap them all for Swift."
http://blog.lexspoon.org/2014/06/my-analysis-of-swift-language.html

Resources