Skip to content

Instantly share code, notes, and snippets.

@jakebrinkmann
Last active March 7, 2024 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakebrinkmann/489f588252d57e19beeded64be0ec315 to your computer and use it in GitHub Desktop.
Save jakebrinkmann/489f588252d57e19beeded64be0ec315 to your computer and use it in GitHub Desktop.
little-go-book

1. Basics

go version
# go version go1.21.5 darwin/arm64

godoc -http=:6060

go run main.go
# it's over 9000!

go run --work main.go
# WORK=/var/folders/y1/jbmj09k53xs164wgqc3mxl080000gp/T/go-build4184586122
# it's over 9000!

go build main.go
./main
# it's over 9000!

go run main1.go
# exit status 1

go run main1.go 9001
# It's over 9001

go run main2.go
# Goku's power is over 9000

2. Structures

go run main4.go
# 9000
# 19000

go run main5.go
# 19001

go run main7.go
# Hi, I am Goku

go run main8.go
# Hi, I'm Goku. Ya!
# Hi, I am Goku

3. Maps, Arrays, Slices

go run main9.go
# 0= 9001
# 1= 9333
# 2= 212
# 3= 33

go run main10.go
## command-line-arguments
#./main10.go:4:2: names declared and not used
#./main10.go:5:2: checks declared and not used
#./main10.go:6:6: names redeclared in this block
#        ./main10.go:4:2: other declaration of names
#./main10.go:7:2: scores declared and not used
package main
func main() {
println("it's over 9000!")
}
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) != 2 {
os.Exit(1)
}
fmt.Println("It's over", os.Args[1])
}
package main
func main() {
names := []string{"leto", "jessica", "paul"}
checks := make([]bool, 10)
var names []string
scores := make([]int, 0, 20)
}
package main
import (
"fmt"
)
func main() {
name, power := "Goku", 9000
fmt.Printf("%s's power is over %d\n", name, power)
}
package main
import (
"fmt"
)
type Saiyan struct {
Name string
Power int
}
func main() {
goku := Saiyan{Name: "Goku"}
goku.Power = 9000
fmt.Printf("%s is %d\n", goku.Name, goku.Power)
}
package main
import (
"fmt"
)
type Saiyan struct {
Name string
Power int
}
func main() {
// CHANGES NOT REFLECTED!
goku := Saiyan{"Goku", 9000}
Super(goku)
fmt.Println(goku.Power)
// USE ADDRESS OF OPERATOR
gokuA := &Saiyan{"Goku", 9000}
SuperA(gokuA)
fmt.Println(gokuA.Power)
}
func Super(s Saiyan) {
s.Power += 10000
}
func SuperA(s *Saiyan) {
s.Power += 10000
}
package main
import (
"fmt"
)
type Saiyan struct {
Name string
Power int
}
func (s *Saiyan) Super() {
s.Power += 10000
}
func main() {
goku := &Saiyan{"Goku", 9001}
goku.Super()
fmt.Println(goku.Power)
}
package main
import (
"fmt"
)
type Saiyan struct {
Name string
Power int
}
func (s *Saiyan) Super() {
s.Power += 10000
}
func NewSaiyan(name string, power int) *Saiyan {
return &Saiyan{
Name: name,
Power: power,
}
}
func main() {
goku := NewSaiyan("Goku", 9001)
goku.Super()
fmt.Println(goku.Power)
}
package main
import (
"fmt"
)
type Person struct {
Name string
}
func (p *Person) Introduce() {
fmt.Printf("Hi, I am %s\n", p.Name)
}
type Saiyan struct {
*Person
Power int
}
func main() {
goku := &Saiyan{
Person: &Person{"Goku"},
Power: 9001,
}
goku.Introduce()
// These are the same:
fmt.Println(goku.Name)
fmt.Println(goku.Person.Name)
}
package main
import (
"fmt"
)
type Person struct {
Name string
}
func (p *Person) Introduce() {
fmt.Printf("Hi, I am %s\n", p.Name)
}
type Saiyan struct {
*Person
Power int
}
func (s *Saiyan) Introduce() {
fmt.Printf("Hi, I'm %s. Ya!\n", s.Name)
}
func main() {
goku := &Saiyan{
Person: &Person{"Goku"},
Power: 9001,
}
goku.Introduce()
goku.Person.Introduce()
}
package main
import ("fmt")
func main() {
scores := [4]int{9001, 9333, 212, 33}
for index, value := range scores {
fmt.Printf("%d= %d\n", index, value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment