How does for and range work together in golang?
Hello guys I am building a CLI tool and I was writing a function that iterates over an array of elements (strings) that hold a filepath and basically just a simple test I wanted to see if I am able at all to open them so I wrote this:
func TestMain (t *testing.T) { files := shared. GoFilesCrawler ("../") for file := range files { fileOp, err := os. Open (file) if err != nil { fmt. Println (err) } fmt. Println ("Able to open the file") defer fileOp. Close () } }
And I was having a trouble because it told me that os.Open() Couldn't accept the argument as an integer, and I was wondering why it was supposing that file is an integer it was weird I mean I had the Go Wiki Range clauses just in front of my eyes and there was nothing talking about this so what I did was
func TestMain (t *testing.T) { files := shared. GoFilesCrawler ("../") for _, file := range files { fileOp, err := os. Open (file) if err != nil { fmt. Println (err) } fmt. Println ("Able to open the file") defer fileOp. Close () } }
And this one works... So my question is why the first argument on the for range must be an integer? Must it be an integer, are there specific cases?
submitted by /u/brocamoLOL
[link] [comments]