The Go Programming Language

2017-08-01  本文已影响0人  keysaim

Notes

Section 2, Program Structure

nested block in if-else if-else block

    if x, y := 100, 200; x > 1000 {
    } else if x := "hello"; y > 0 { //this x shadow the 'x' in if
        fmt.Println(x, y)
    }

scope shaw issue

    var cwd string
     func init() {
         cwd, err := os.Getwd() //compile error: the 'cwd' declared but not used
         if err != nil {
             log.Fatalf("os.Getwd failed: %v", err)
         }
    }

'cwd', 'err' are not declared in their block, so the compiler will declare them and will shadow the global 'cwd' variable.

Section 3, Basic Data Type

Go types

Types synonym

Operators

    fmt.Println(-5 % -2) //-1
    fmt.Println(5 % -2) //1
    fmt.Println(-5 % 2) //-1
    var u uint8 = 255
    fmt.Println(u, u+1, u*u) // "255 0 1"
    var i int8 = 127
    fmt.Println(i, i+1, i*i) // "127 -128 1"
    nan := math.NaN()
    fmt.Println(nan == nan, nan < nan, nan > nan) // "false false false"

Conversion and format

Printf

    o := 0666
    fmt.Printf("%d %[1]o %#[1]o\n", o) // "438 666 0666"
    x := int64(0xdeadbeef)
    fmt.Printf("%d %[1]x %#[1]x %#[1]X\n", x)
    // Output:
    // 3735928559 deadbeef 0xdeadbeef 0XDEADBEEF

Unicode

enumeratio

const (
    _ = 1 << (10 * iota)
    KiB // 1024
    MiB // 1048576
    GiB // 1073741824
    TiB // 1099511627776 (exceeds 1 << 32)
    PiB // 1125899906842624
    EiB // 1152921504606846976
    ZiB // 1180591620717411303424
    YiB // 1208925819614629174706176
)

The 'iota' is 'int' type, so it will overflow.

Section 4, Composite Types

About array type

About the slice type

About the map type

About the struct type

About the JSON

Section 4, Functions

Function definition

Go function stack

About the defer

Section 6, Methods

About the receiver

About the embedding

About the encapsulation

Section 7, Interfaces

About the interface satisfaction

interface values

Type assertion

Type switch

Section 8, Goroutines and Channels

Channels

Section 9, Concurrency with Shared Variables

Mutex

Goroutines vs OS threads

Section 10, Packages and The Go Tool

Go build

Go doc

Internal packages

Go list

Section 11, Testing

Tests

External Test Package

Benchmark Testing

Profiling

Example Testing

Section 12, Reflection

reflect, type and value

上一篇 下一篇

猜你喜欢

热点阅读