Go's equivalent to Python modules are packages. Unlike Python modules that exist as a single file, a Go package is represented by a directory. While a simple package might only include a single source file in the directory, larger packages can be split over multiple files.
So to take your Python example, you could create a file $GOPATH/src/a/a.go
with the following contents:
package a
import "fmt"
func Foo() {
fmt.Println("a.Foo")
}
In your main program, you can call the function like so:
package main
import "a"
func main() {
a.Foo()
}
One thing to note is that only exported types and functions in a
will be available externally. That is, types and functions whose names begin with a capital letter (which is why I renamed foo
to Foo
).
If you don't want to set up a Go workspace, you can also use relative paths for your imports. For instance, if you change the import in the main program to import "./a"
, then the source code for the a
package can be located at a/a.go
relative to the main program.