Want to learn how to build better Go applications faster and easier? You can.
Check out my course on the Go Standard Library. You can check it out now for free.
So, you’re probably wondering how to create a Go project. Like, how do people just start a project?
It’s easy. Open a command prompt.
Make a directory, in my case I named it “test”
use Go init to start a new project. You can make up the repo name, or if you actually have a Git repo for it, use that.
go mod init example.com/test
This will create a project and a go.mod
file for you. You can use this to manage dependencies:
Now, create a main.go
with some hello world code:
And save it.
Now you can type
go build
and build your file. Because I named mine example.com/test the binary it makes will be “test”:
And you can see the results of my file. It’s easy!!
You can also run the individual file:
go run main.go
but here’s an even better way. Using “Make”. Create a file named makefile
that looks like this:
build:
go build
run: build
./test
Save it, and type:
make run
This file will build, then run your application.
The advantage of doing this is, you can add environment variables, or complicated build strings and save them in this file, so running it super easy.
I hope this has helped.
Thanks for reading, and good luck on your Go adventures!