Skip to content

Various snippets of Go code from following tutorials and courses

Notifications You must be signed in to change notification settings

sajidk1/snippets-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

snippets-golang

This repo contains various snippets of Go code (./snippets) and serves as an example Go application repo (main.go, modules, go.mod, go.sum)

Install Go

brew install go or grab a binary for your OS here

Read docs

A short read (I promise), walking you through a "Hello, World!" example.

Run a snippet

cd snippets
go run hello_world.go

Run the main program

In the root of the repo

go run main.go

Build the main program i.e. create a binary

In the root of the repo

go build

Test functions

cd modules/greetings
go test -v

Format all code

gofmt -w *.go

Further reading

Useful links

Modules

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

Using local packages

  • Create a new folder called greetings in ./modules

    • Create a file called greetings.go in the new directory
    • Add the line package greetings
  • 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
    • or in your go.mod file you can reference a local copy:
      • replace github.com/sajid-khan-js/snippets-golang/modules/greetings => ./modules/greetings

Further reading

About

Various snippets of Go code from following tutorials and courses

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages