A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.
func main() { … }
Program execution begins by initializing the main package and then invoking the function main . When that function invocation returns, the program exits. It does not wait for other (non- main ) goroutines to complete.
Notice the first sentence here says single.
Golang needs one main for it to start up, run, and close the program.
Main is also not imported anywhere so even if you wanted to make another Go file at the main package scope there is no way to import the code.
If you need to have a.go in its own file then create a package with a new name, export the struct and function, then import that new package into main.go.
Otherwise, add the struct and method to main.go and remove a.go.
Single File Example:
package main
import "fmt"
func main() {
d := su{"test string", 2}
d.do()
}
type su struct {
Sfd string
Sf int
}
func (sed *su) do() {
fmt.Printf("Sfd: %s\nSf: %d", sed.Sfd, sed.Sf)
}
Technically, there’s nothing wrong with having several files in the main package and it should work without issues (e.g. dockerd), as it does in your playground example. I’ve also checked it locally in vscode and there’s no problem either, so I’m guessing you have something misconfigured at your install.
@iegomez I tried to split the main package but I am also getting compilation error(undefined su). I have referred the dockerd link you have shared and there no special handling in that link. I am wondering how it is working for you(and in dockerd) but not working in our local environment. Can you please explain is there any additional configuration we need?
thanks bro for your answer. i have been playing golang for almost one year and i feel it’s too late to meet it. recently, i found this problem, and i think it’s interesting, but i dont understand why, so i seek answer in this big family
My best guess would be that it’s related to configuration of GOPATH or go mod at vscode in some way.
I mean, the original example builds and runs without a problem, you can try it yourself with go build from a terminal. I copied the exact same files an threw them in a test dir and all I had to do was go mod init (as I have modules enabled by default), go build and ./test to get it to run, either from a separate terminal or from one opened at vscode. I also don’t have anything special installed except the Go language support extension, and I do get hints and everything is properly recognized when coding, e.g., if I type d. at file2.go, vscode suggests the do method and both Sfd and Sf for autocompletion.
i set the gopath and goroot as environment variables. and i run it in another powershell. the problem is still . it is strange that the vscode always can recognized when i code the struct. but when i go run/build it the problem arises.