Go Packages
Code is organized by packages in Go. They are similar to Python's modules and JavaScript's npm modules.
What is main
main
The most common package in Go is main
because main package is served as an entry point to any Go program.
For example:
This will not compile and throw an error. You must call your functions inside the main
function.
Non-main Packages
We can write our own packages. However, we cannot run a non-main package directly. We must import it into the main
package. For example, create a new folder in helloworld
and call it foo
. Inside the folder, make a file called foo.go
.
Now we can import the package into the main program and run foo.SayHello
.
Last updated