Skip to content

Instantly share code, notes, and snippets.

@israelb
Last active November 1, 2017 02:30
Show Gist options
  • Save israelb/3bd9f4727dcec92cc873bfce10e3a6aa to your computer and use it in GitHub Desktop.
Save israelb/3bd9f4727dcec92cc873bfce10e3a6aa to your computer and use it in GitHub Desktop.
Golang

The Go Programming Language

Go Programming Blueprints - Second Edition

Go in Action

Go in Practice

Building Microservices with Go

Go: Design Patterns for Real-World Projects

Go: Building Web Applications

reference: The Go Programming Language

Composite Types:

ARRAYS

// array of 3 integers
// the array literal is: [3]
var a [3]int  

var myVar32Bytes [32]byte

We can use an array literal to initialize an array with a list of values

var q [3]int = [3]int{1, 2, 3}  
q := [...]int{1, 2, 3}          // same as before
q := [3]int{1, 2, 3}            // same as before

Is also possible to specify a list of index and value pairs, like this

type Currency int

const (
    USD Currency = iota
    EUR
    GBP
    RMB
)

symbol := [...]string{USD: "$", EUR: "€", GBP: "£", RMB: "¥"}

fmt.Println(RMB, symbol[RMB]) // "3 ¥"

Defines an array r with 100 elements, all zero except for the last, which has value −1.

r := [...]int{99: -1}

SLICES

It looks like an array type without a size A slice has three components: a pointer, a length, and a capacity

months := [...]string{1: "January", /* ... */, 12: "December"}

Q2 := months[4:7]
summer := months[6:9]
fmt.Println(Q2)     // ["April" "May" "June"]
fmt.Println(summer) // ["June" "July" "August"]

The built-in function make creates a slice of a specified element type, length, and capacity

make([]T, len)
make([]T, len, cap) // same as make([]T, cap)[:len]

MAPS

A map is a reference to a hash table, and a map type is written map[K]V, where K and V are the types of its keys and values

ages := make(map[string]int) // mapping from strings to ints

We can also use a map literal to create a new map populated with some initial key/value pairs:

ages := map[string]int{
    "alice":   31,
    "charlie": 34,
}

Equivalent to:

ages := make(map[string]int)
ages["alice"] = 31
ages["charlie"] = 34

Map elements are accessed through the usual subscript notation:

ages["alice"] = 32
fmt.Println(ages["alice"]) // "32"

and removed with the built-in function delete:

delete(ages, "alice") // remove element ages["alice"]

The order of map iteration is unspecified. To enumerate the key/value pairs in order, we must sort the keys explicitly, for instance, using the Strings function from the sort package if the keys are strings. This is a common pattern:

example: https://play.golang.org/p/n3JdCTj45R

POINTERS

A pointer value is the address of a variable. A pointer is thus the location at which a value is stored. With a pointer, we can read or update the value of a variable indirectly, without using or even knowing the name of the variable

func main() {
    var x int
    x = 20
    
    fmt.Println(&x)  // address of x
	p := &x

	// The variable to which p points is written *p.
	fmt.Println(*p)  // p contains of address of x

	*p = 2 // equivalent to x = 2

	fmt.Println(*p)
	fmt.Println(x)

	fmt.Println(&x)  // address of x

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment