This repo contains various snippets of Go code (./snippets
) and serves as an example Go application repo (main.go
, modules
, go.mod
, go.sum
)
brew install go
or grab a binary for your OS here
A short read (I promise), walking you through a "Hello, World!" example.
cd snippets
go run hello_world.go
In the root of the repo
go run main.go
In the root of the repo
go build
cd modules/greetings
go test -v
gofmt -w *.go
- https://www.youtube.com/watch?v=Q0sKAMal4WQ
- https://www.youtube.com/watch?v=C8LgvuEBraI&feature=youtu.be
- https://golang.org/doc/tutorial/
- https://tour.golang.org/list
- https://gowebexamples.com/
- https://golang.org/doc/effective_go#introduction - Go best practices
- https://golang.org/doc/code.html
- https://medium.com/appsflyer/my-journey-from-python-to-go-3859783c6b3c
- https://www.dotnetperls.com/
- https://www.one-tab.com/page/SIUmuC-BR2StIaxHdNX5Rw - Linters and pre-commit
How todo dependency management in Go
Initialise the module (run this at the root of a repo):
go mod init github.com/sajid-khan-js/snippets-golang
This creates a go.mod
file.
Next time you run your code, you'll get:
❯ go run main.go
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2
...
This updates the go.mod
file with any dependencies (i.e. anything listed under
import
in your code that isn't a standard package). It also creates a go.sum
file which contain cryptographic checksums of the content of specific module
versions.
Both go.mod
and go.sum
should be checked into Git alongside your code
-
Create a new folder called
greetings
in./modules
- Create a file called
greetings.go
in the new directory - Add the line
package greetings
- Create a file called
-
To import that package into another program:
- import
"github.com/sajid-khan-js/snippets-golang/modules/greetings"
- 📝 Your module (i.e. the root
go.mod
) needs to be pushed to your Git repository for this to work
- 📝 Your module (i.e. the root
- or in your
go.mod
file you can reference a local copy:- replace
github.com/sajid-khan-js/snippets-golang/modules/greetings => ./modules/greetings
- replace
- import