How to split package across files

I wish to split the code in the “main” package across 2 files, but I get an undefined error for the maps I have placed in the second file. I was under the impression that all that was required was a package main statement at the top of the second file but evidently not.
Can anyone help?

Remember that the first letter goes capitalized if you want the identifier will be public to any piece of code that want to use it.

I have 2 files : main.go and mainconst.go. Both start with the statement “package main”

I laboured under the misconception that all I needed was to have “package main” as the first statement and I could then execute go run main.go

After some googling I came upon this post : https://stackoverflow.com/questions/28153203/golang-undefined-function-declared-in-another-file

" For go run, you need to supply all files as argument" so go run main.go mainconst.go does the job

2 Likes

Or if there are no other go files in the directory can you do

$ go run *.go
1 Like

Works only in posix compatible shells, not with windows cmd.exe. I’m not sure though who is responsible for glob-expansion in a powershell…

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