Course: Go: The Complete Developer's Guide (Golang) - Stephen Grider

March 14th, 2023

udemy.com/course/go-the-complete-developers-guide

 

https://github.com/StephenGrider/GoCasts

 


 

Hello world

package main

 

 

import "fmt"

 

 

func main(){

fmt.Println("Hello world!")

}

 

go run main.go

 


 

go build <files>    # compile files

go run <files>    # compile + execute

go fmt    # format all files in PWD

 

# dependencies:

go install    # install a package

go get    # download raw src coude

 

go test    # run tests

 


package ~= project

 

package main -> executable

package not_main -> library

 

package main MUST have

func main(){}

 


 

golang.org/pkg

available in the standard library

 


Go Types

# full declaration

var my_name string = "foo bar"

 

# shorthand

my_name := "foo bar"

 

raw types

  • bool

  • string

  • int

  • float64

 

lists

  • array // fixed length

  • slice // variable size

// slice

foo := []string{"a", "b"}

foo = append(foo, "c")

 

// array

 

 

maps

  • map

 


 

custom types

 

// cf type alias

type deck []string

 

functions with 'deck' as a 'receiver'

// cf type extensions

func (d deck) print() {

for i, card := range d {

fmt.Println(i, card)

}

}

 

cards.print()

 

 

asciitable.com

 


 

type casting

[]byte("foo bar")

 


 

Unit Testing

xxx_test.go

 

 

Go error: cannot find main module, but found .git/config

 

Run intelli/golandj files without having to add each file manually

 

[ ] ? is there really no other way to write test for go? no asserts or anything?

 


 

Structs

type person struct {

firstname string

lastname  string

}

 

alex := person{"Alex", "Anderson"}

// or

blex := person{firstname: "Blex", lastname: "Anderson"}

 

 

print fieldnames + values

fmt.Printf("clex: %+v", clex)

> clex: {firstname: lastname:}

 


 

Pointers

"Go is a pass by value language"

[x] structs are pass by value? (instead of pass by reference)

 

(&clex).updateFirstName("NEW")

// or shorter:

clex.updateFirstName("NEW")

 

func (p *person) updateFirstName(newFirstName string) {

(*p).firstname = newFirstName

}

 

&variableName    // return memory address for variable

*pointerName    // return value for memory adress

*typeName    // type description

 

[ ] slices/arrays are pass by reference?

yes and no? -> they seem to act like pass by reference

slice = struct {

pointer to head    // pointer to array

capacity    // array size

length    // current length

}

 

Reference types

// behave like pass by reference

  • slices

  • maps

  • channels

  • pointers

  • functions

 


 

Maps

 

colors := map[string]string{

"red":  "#ff0000",

"blue": "#0000ff",

}

//or

colors := make(map[string]string)

//or

var colors map[string]string

 

 

colors["blue"]

colors["white"] = "#fffff"

delete(colors, "red")

 


Interfaces

 

type bot interface {

getGreeting(string, int) (string, error)

}

 

func printGreeting(b bot) {

fmt.Println(b.getGreeting("", 0))

}

 

 

type englishBot struct{}

func (eb englishBot) getGreeting(_ string, _ int) (string, error) {

return "Hi there!"

}

 

concrete type vs interface type

cannot (directly) create an instance from an interface    // cf java cannot new an abstract class (without creating an anonymous)

 

Go has no support for generics

 

 

nested interfaces

type ReadCloser interface {

Reader

Closer

}

type Reader interface {

Read(p []byte) (n int, err error)

}

type Closer interface {

Close() error

}

(src)

 

 


HTTP

 

resp, err := http.Get("https://example.com")

io.Copy(os.Stdout, resp.Body)

 


Concurrent programming

  • Channels

  • Go Routines

 

Go Routine

# add go in front of a function call

go checkLink(link)

-> no output, because the main routine stops before any child routine is printing

 

Channel

single type

c := make(chan string)

go checkLink(link, c)

message <- c    // blocking call

 

func checkLink(link string, c chan string) {

c <- link // blocking call

}

 

looping

for {

go checkLink(<-c, c)

}

//or

for l := range c {    // cleaner

go checkLink(l, c)

}

 


Function Literal (aka lambda)

func(){...}()

 

fix closure problems

func(foo string){...}("hello")