Package confusion

I thought I had this figured out but must be missing something.

main file: test/test.go
package main

I then created another file in the same folder called test2.go and added a single variable to it.
It also has package main in it at top of file.

I thought because they were in same folder with same package name that I could call things in them but it is not working. So the variable I defined in the test2.go main package isn’t seen in the test.go file main package.

What am I missing?

Glenn

The code for my test app
folder test
test.go

package main

import (
 "fmt"
)

func main() {
  fmt.Print("Hello world\n")
  var ms mystruct  //error happens here with don't know what a mystruct is

}

test2.go code in same folder with same package name

package main
type mystruct struct {
   var1 string
   var2 string
}

Hi. Did you run it with go run test.go test2.go? Or go build -o mytest test.go test2.go? Or was it you editor complaining?

I didn’t realize I needed to list each file in the folder. All documentation I’ve read and examples just says it uses whatever is in the folder that shares that package name.

Thanks, that fixed my issue.

Glenn

This is true, if the package name is not main

ok, so to be clear, if I have separate files that I break apart within the “main” package folder then I will need to list them specifically when compiling. But if I include a folder into a main package and that folder happens to have 10 files in it, they do NOT need to be listed individually?

Kinda surprising it works like that but just as long as I know.

Thanks,

Glenn

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.