Home > Software design >  Go file not found if moved to folder
Go file not found if moved to folder

Time:05-11

I am new to golang.

If I work with main.go on the main folder in VS code, no problem. But when I move main.go to a sub-folder and try to running on Terminal i get this message:

"CreateFile main.go: The system cannot find the file specified."

What could i have made wrong?

Thanks

CodePudding user response:

Usually the main.go file is kept in a directory called cmd.

In golang you cannot have files that belong to different packages in the same dir.

main.go belongs to the main package so it is best practice to place it in a separate directory.

CodePudding user response:

After watching the attached video:

  1. go run main3.go gives error since it is inside the folder 02variables and not in your present working directory. Try go run 02variables/main3.go
  2. Your GOPATH is set to your project folder which is not good. You can set it to nothing. GOPATH is an outdated mechanism anyway since Go has switched to modules. Read more here
  3. Each Go module can only contain one main. So depending on your requirement you will have to create each folder as a separate module or look into sub-modules
  4. If you are just starting out with go and looking to learn, I would suggest start with Tour of Go. Seems like the tutorial you are following isn’t great / outdated
  •  Tags:  
  • go
  • Related