I'm trying to figure out how to (or if it's possible to) combine multiple assignment and ranges in Golang
ex pseudo code of what I'd like to do
files := [2]*os.File{}
for i, _, fileName := 0, range os.Args[1:3] {
files[i], _ = os.Open(fileName)
}
The idea being I want to have both an iteration counter (i
) and the filenames (fileName
). I know this can be achieved by using the key from range and some math (key -1
), thats not the point of the example.
Edit:
Upon debugging the above example, I learned that i
will range 0-1 in that example; Because os.Args[1:2] is a slice and that slice has indexing 0-1 . Therefore I dont need "some math" to properly index the keys.
** EDIT 2: **
This post is also a must read as to why the above [2]*os.File{}
is not idiomatic go, instead it should not have a size specified (files := []*os.File{}
) so that files
is of type slice of *os.File