The Go Programming Language

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

  • basic types: numbers, strings, booleans
  • aggregate types: arrays and structs
  • reference types: slices, maps, functions, channels. (Each includes pointers that point to internal data)
  • interface types

Types synonym

  • int/uint types' size are platform and compiler dependent. Never make assumption for the size!
  • 'rune' is synonym to 'int32', while 'byte' to 'uint8'
  • 'uintptr' is for the low level programming, like Go with C

Operators

  • the '%' for negative:
    fmt.Println(-5 % -2) //-1
    fmt.Println(5 % -2) //1
    fmt.Println(-5 % 2) //-1
  • the result is the same type as the operators, so overflow may happen
    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"
  • 'unary operators': '+', '-', '^'

    ** For integers, '+x' is short for '0 + x' while '-x' is short for '0 - x'
    ** For floating and complex numbers, '+x' is just for 'x' while '-x' is the negative of 'x'
    ** '^x' return the value with each bit inverted

  • 'NaN' is from like '0/0' or 'sqrt(-1)'. Any comparation of 'NaN' always yield false

    nan := math.NaN()
    fmt.Println(nan == nan, nan < nan, nan > nan) // "false false false"
  • Row string literal: no escape happens and can cross multiple lines

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
  • The '[1]' means use the first argument, so no need to provide the same argument again and again

  • The '#' is used to add the '0', '0x', '0X'

  • If space is after the '%', like '% x', then it will insert the space for each hex digits, like 'e3 83 97 e3 83 ad e3 82 b0 e3 83 a9 e3 83 a0'

  • The 'strconv' package includes many format functions

  • The '%t' show true or false, '%T' show the type

Unicode

  • Unicode version 8 use 4 bytes for each charactor, also knows as UTF-32/UCS-4. In Go, the 'rune' is used for this.

  • Unicode wasts lots of space, so the 'UTF-8' is invented.

    00xxxxxx runes0?127 (ASCII)
    111xxxxx 10xxxxxx 128?2047 (values <128 unused)
    1110xxxx 10xxxxxx 10xxxxxx 2048?65535 (values <2048 unused)
    11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 65536?0x10ffff (other values unused)
    
  • Code point

    Same values:

    "世界"
    "\xe4\xb8\x96\xe7\x95\x8c" //the binary using the UTF-8 coding
    "\u4e16\u754c" //the binary using the Unicode, will be encoded as UTF-8 by the compiler
    "\U00004e16\U0000754c" //the binary using the Unicode, will be encoded as UTF-8 by the compiler
    

    They are all valid UTF-8 encoding of code point, but in Go, when for 'rune', value below 256 can used as '\x', while above 256, must use '\u' or '\U'.

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

  • The size is part of the type, so '[3]int' is different type from '[5]int'

  • Can init with specific indices

    symbol := [...]string{0: "$", 2: "9", 5: "!", 9: """}
    r := [...]int{99: -1}
    

    All the ones not in the indices will be init as the zero value.

  • Arrays are comparable, 'equal' when the two arrays have the same type and all the elements are the same

About the slice type

  • Index beyonds the capcity will cause panic, while beyonds length will expend the slice

  • 's[i:j]' will share the same underlying array with slice 's'

  • Slices are not comparable. For '[]byte], using the 'bytes.Equal' to compare

  • Nil slice has zero length and zero capcity, while the reverse is not right.

    var s []int    // len(s) == 0, s == nil
    s = nil        // len(s) == 0, s == nil
    s = []int(nil) // len(s) == 0, s == nil
    s = []int{}    // len(s) == 0, s != nil
    
  • Nil slice and non-nil slice with zero length should be treated in the same way, so using 'len(s) == 0' to check if a slice is empty

  • Under the hood, 'make' creates an unnamed array variable and returns a slice of it

  • With append, slice may enlarge its space to hold new elements. The copy will handle the overlap of the underlying array.

About the map type

  • Lookup using a key not existing will return the zero value of the value type.

    a := make(map[string]int)
    a["bob"] = a["bob"] + 1
    fmt.Println(a["bob"]) //will output 1
    
  • You can use the '++' to increase the value

    a := make(map[string]int)
    a["bob"]++
    fmt.Println(a["bob"]) //will output 1
    
  • A map element is not variable, you can never try to get its address. But on the different, you can get the address of a slice element

    _ = &a["bob"] //compile error
    
  • Zero value of the map is nil.

    var ages map[string]int
    fmt.Println(ages == nil)    // "true"
    fmt.Println(len(ages) == 0) // "true"
    
  • A nil map supprts operations: lookup, delete, len, range, but not store value.

    var aa map[string]int
    aa["bob"] = 100 //panic, the map must be created before storing any value
    
  • The maps are not comparable except for comparision with nil.

About the struct type

  • Will compile error

    func create(id int) Employee {
    //...
    }
    create(100).Name = "bob" //compile error, change the return type to *Employee will be ok
    
  • Fields not exported in a struct cannot be inited in another package

    package p
    type T struct{ a, b int } // a and b are not exported
    
    
    package q
    import "p"
    var _ = p.T{a: 1, b: 2} // compile error: can't reference a, b
    var _ = p.T{1, 2}       // compile error: can't reference a, b
    
    

    And this is also wrong:

    package p
    type T struct{ A, b, C int } // a and b are not exported
    
    
    package q
    import "p"
    var _ = p.T{1, 2}       // compile error: can't reference A, b
    var _ = p.T{A: 1, C: 2} // ok
    
  • Embedding will form an anoymous field of the struct with the implicit field name of the embedded type.

    type Point struct {
        X, Y int
    }
    
    type Circle struct {
        Point // form a anoymous field with implicit field name "Point"
        Radius int
    }
    
    type Wheel struct {
        Circle // form a anoymous field with implicit field name "Circle"
        Spokes int
    }
    

    So you cannot embed the same type for more than twice, as their implicit names will conflict.

  • The implicit name can be optional for dot expression

    w := new(Wheel)
    w.Circle.Radius = 100
    w.Radius = 100 // Both of the two are valid
    
  • The embedded struct cannot init by normal struct literal

    w = Wheel{8, 8, 5, 20}                       // compile error: unknown fields
    w = Wheel{X: 8, Y: 8, Radius: 5, Spokes: 20} // compile error: unknown fields
    

    You must init like this:

    w = Wheel{Circle{Point{8, 8}, 5}, 20}
    w = Wheel{
      Circle: Circle{
           Point:  Point{X: 8, Y: 8},
      Radius: 5,
      },
      Spokes: 20, // NOTE: trailing comma necessary here (and at Radius)
    }
    fmt.Printf("%#v\n", w)
    // Output:
    // Wheel{Circle:Circle{Point:Point{X:8, Y:8}, Radius:5}, Spokes:20}
    w.X = 42
    fmt.Printf("%#v\n", w)
    // Output:
    // Wheel{Circle:Circle{Point:Point{X:42, Y:8}, Radius:5}, Spokes:20}
    

About the JSON

  • field tag is a string of metadata associated at compile time with the field of a struct. It's a 'key:"value"' pair.

    Year  int  `json:"released"`
    Color bool `json:"color,omitempty"`
    
  • The JSON field tag format: json:"<json name>,[addition option]"

  • When Marshal, the not exported fields will be ignored; When Unmarshal, the fields of JSON that are not in the data struct will be ignored

  • Associating JSON names with Go struct names during Unmarshaling is case-insensitive, so no need to add the JSON field tag for simple field name. But for the Marshal, you must define the JSON field tag, or the capital name will be used.

Section 4, Functions

Function definition

  • If return only one unamed type, the '()' can be omitted in the return result definition.

  • Parameters and named results share the same level of the function outermost block.

Go function stack

  • Go has a variable size function stack, so the recursive is always safe.

About the defer

  • The 'defer' forms a stack and wil be called by stack before the function return.

  • Even panic in the function, the 'defer' will be called too.

Section 6, Methods

About the receiver

  • The compiler will perform implicit '&p' on the variable when the receiver is the pointer.

    type Point struct {
        x int
        y int
    }
    
    func (p *Point) Offset(off int) {
        p.x += off
        p.y += off
    }
    
    //...
    p := Point{}
    p.Offset(10) // This is valid, as the compiler will perform implicit '&p' on this.
    Point{1, 2}.Offset(100) // compile error, as it's not the variable and cannot be addressed.
    
  • It's true on these three scenarios:

    1. The type of the variable is the same as the receiver parameter
    2. The type of the variable is T, while the receiver parameter is *T (The compiler will implicitly perform '&p')
    3. The type of the variable is T, while the receiver parameter is T (The compiler will implicitly perform 'p')
  • Bind the method with the receiver and assign to a variable, then you can call the method without the receiver via the variable. This is called method value

    p := Point{1, 2}
    q := Point{4, 6}
    distanceFromP := p.Distance
    fmt.Println(distanceFromP(q))
    
  • Assigning the method to variable directly is called method expression, you can then call it by providing the receiver as the first parameter.

    p := Point{1, 2}
    q := Point{4, 6}
    distance := Point.Distance   // method expression
    fmt.Println(distance(p, q))  // "5"
    

About the embedding

  • Embedding a type will inherit all its methods. In terms of implementation, the compiler will generate the wrapped methods with the type.

About the encapsulation

  • All fields in the struct is visible to any function or any method in the same package

Section 7, Interfaces

About the interface satisfaction

  • Though you can use the type T to access *T method (the compiler will perform it implicitly), but you cannot use type T to satisfy the *T type interface.

interface values

  • An interface value is composed of: dynamic type and dynamic value. The type is the dynamic type of the value.

    var w io.Writer // both the type and value are nil
    if w == nil { // this is true
        fmt.Println('is nil')
    }
    
    var buf *bytes.Buffer // buf is a nil pointer
    w = nil // both the type and value are nil
    w = buf // the type is '*bytes.Buffer' (not nil), while the value is nil
    if w == nil { // this is false, as the type of w is not nil, so the w is not nil
        fmt.Println('is nil')
    }
    
    
  • interface value comparation may cause panic

    var x interface{} = []int{1, 2, 3}
    fmt.Println(x == x) // panic: comparing uncomparable type []int
    

    When comparation, both the type and value must be comparable. Similar risk eixsts when using the interfaces as the map keys.

  • To report the dynamic type of an interface value, using the '%T'

    var w io.Writer
    fmt.Printf("%T\n", w) // "<nil>"
    w = os.Stdout
    fmt.Printf("%T\n", w) // "*os.File"
    w = new(bytes.Buffer)
    fmt.Printf("%T\n", w) // "*bytes.Buffer"
    

Type assertion

  • x.(T), if T is a concrete type, it will check if x's dynamic type is identical to T, if so the results is the dynamic value, or it will panic. If the T is a interface type, it will first check if x's dynamic type satisfies T, if so, a new interface value will be returned with the new dynamic type T and the same dynamic value.

  • No matter what type was asserted, if the operand is a nil interface value, the type assertion fails

Type switch

  • take one example, you can assign the x.(type) to a new variable

    func sqlQuote(x interface{}) string {
             switch x := x.(type) {
             case nil:
                 return "NULL"
             case int, uint:
                 return fmt.Sprintf("%d", x) // x has type interface{} here.
             case bool:
                 if x {
                     return "TRUE"
                }
                 return "FALSE"
             case string:
                 return sqlQuoteString(x) // (not shown)
             default:
                panic(fmt.Sprintf("unexpected type %T: %v",wxw,wx.i)t)-ebooks.info }
    }
    

Section 8, Goroutines and Channels

Channels

  • The channel is a reference.

  • channels are comparable, it's true when both are references to the same channel data structure.

  • A channel can be closed, send to a closed channel will cause panic, while receive on a closed channel, it will yield zero value of the channel element type after the channel is empty.

  • To test on a closed channel, use like x, ok := <- c, to be comvinent, you can use the 'range' to loop the channel until it's drain and closed.

    for x := range naturals { // the loop will exit when the channel 'naturals' is drained and closed.
        squares <- x * x
    }
    
  • It's not necessary to close the channel when you finish with it. Only close it when you want to tell the receive that you will sent anymore data. It's different from the file, you must always close a file after finishing with it.

  • Unidirectional channels: the chan<- int is send-only channel, the <-chan int is the receive-only channel. And the close must not be applied on the receive-only channel. It will implicitly convert chan int to chan<- int and <-chan int.

  • To get the capcity of the channel, using the cap(c), while the len(c) returns the currently number of the buffered elements.

  • An typical example for loop paralle

    func makeThumbnails6(filenames <-chan string) int64 {
             sizes := make(chan int64)
             var wg sync.WaitGroup // number of working goroutines
             for f := range filenames {
                 wg.Add(1)
                 // worker
                 go func(f string) {
                     defer wg.Done()
                     thumb, err := thumbnail.ImageFile(f)
                     if err != nil {
                         log.Println(err)
                             return
                             }
                     info, _ := os.Stat(thumb) // OK to ignore error
                     sizes <- info.Size()
                 }(f)
             }
             // closer
             go func() {
                 wg.Wait()
                 close(sizes)
             }()
             var total int64
             for size := range sizes {
                 total += size
             }
             return total
         }
    
  • Using a buffered channel as a semaphore

    var sema = make(chan struct{}, 10)
    func dowork() {
        sema <- struct{}{} // acquire the sema
        defer func() { <-sema }() // release the sema
        // do something here
        //...
    }
    
  • Using select with a closeable channel to cancel the goroutines. Closing the channel is like broadcasting the goroutines to let them exit gracefully.

    func main() {
        done := make(chan struct{})
        jc := make(chan string)
        go func() {
            select {
            case <-done:
                return
            case job := <-jc:
                //do something with the job
            }
        }()
    
        jc<- "hello"
        close(done)
    }
    

Section 9, Concurrency with Shared Variables

Mutex

  • defer will be be executed even the func panic

  • The mutex is not re-entrant, which means that it can not be locked recursively.

  • Different goroutines may run on different CPU, different statements may be reordered by the modern compiler and CPU when they are not dependent on each other.

         var x, y int
         go func() {
             x = 1 // A1
             fmt.Print("y:", y, " ") // A2
         }()
         go func() {
             y = 1                   // B1
             fmt.Print("x:", x, " ") // B2
         }()
    

    The result may be "x = 0, y = 0", as the CPU or compiler may reorder the statements; the different goroutines may run on different CPU, and the update may happen on each CPU cache and doesn't sync with main memory on time. The mutex will make sure the right order.

  • Duplicate suppression

    type entry struct {
             res   result
             ready chan struct{} // closed when res is ready
         }
    
    func New(f Func) *Memo {
         return &Memo{f: f, cache: make(map[string]*entry)}
    }
    
    type Memo struct {
        f     Func
        mu    sync.Mutex // guards cache
        cache map[string]*entry
    }
    
    func (memo *Memo) Get(key string) (value interface{}, err error) {
        memo.mu.Lock()
        e := memo.cache[key]
        if e == nil {
            // This is the first request for this key.
            // This goroutine becomes responsible for computing
            // the value and broadcasting the ready condition.
            e = &entry{ready: make(chan struct{})}
            memo.cache[key] = e
            memo.mu.Unlock()
            e.res.value, e.res.err = memo.f(key)
            close(e.ready) // broadcast ready condition
        } else {
            // This is a repeat request for this key.
            memo.mu.Unlock()
            <-e.ready // wait for ready condition
        }
        return e.res.value, e.res.err
    }
    

Goroutines vs OS threads

  • Goroutine has dynamic size of stack, which is up to 1GB while the OS thread is typical 2MB.

  • Goroutines schedule implicitly by certain Go language constructs, like time.Sleep, channel block, mutex block, etc, no need to switch kernel context. While the OS threads schedule is invoked every few ms, need to switch kernel context.

  • GOMAXPROCS is the default the number of the CPU on the machine.

Section 10, Packages and The Go Tool

Go build

  • Using flag '-u' in 'go get' will update the current repo to the latest version.

  • The 'go build -i' will install the dependent packages and will decrease the compile time next time.

  • Cross compile

    $ GOARCH=386 go build -i gopl.io/ch10/cross // it will install in $GOPATH/pkg/386
    
  • Files like net_linux.go or asm_amd64.s, the compiler will compile it based on the env.

  • Comment before the file package declaration can also control the compiling. // +build linux darwin means only compile it for linux and darwin, // +build ignore means never compile this file.

Go doc

  • The first sentence is usually a summary that starts with the declared name.

    // Fprintf formats according to a format specifier and writes to w.
    // It returns the number of bytes written and any write error encountered.
    func Fprintf(w io.Writer, format string, a ...interface{}) (int, error)
    
  • Check the doc

    $ go doc json.encode
    func (dec *Decoder) Decode(v interface{}) error
        Decode reads the next JSON-encoded value from its input and stores
        it in the value pointed to by v.
    
  • Export the doc to html and serve it

    $ godoc -http :8000
    

    And then, access the doc via http://localhost:8000/pkg

Internal packages

  • package under the 'internal' directory is the internal package

  • Can be seen by the packages under the parent of the 'internal' directory

    net/http
    net/http/internal/chunked
    net/http/httputil
    net/url
    

    The 'net/http/internal/chunked' can be seen by 'net/http', 'net/http/httputil', while not seen by 'net/url'

Go list

  • list the packages

  • List all the packages under the workspace.

    $ go list ...
    
  • List all under path

    $ go list gopl.io/ch3/...
    
  • List by matched pattern

    $ go list ...xml...
    encoding/xml
    gopl.io/ch7/xmlselect
    
  • go list -json to show by json format of the package detail info.

    $ go list -json hash
    {
        "Dir": "/home/gopher/go/src/hash",
            "ImportPath": "hash",
            "Name": "hash",
            "Doc": "Package hash provides interfaces for hash functions.",
            "Target": "/home/gopher/go/pkg/darwin_amd64/hash.a",
            "Goroot": true,
            "Standard": true,
            "Root": "/home/gopher/go",
            "GoFiles": [
                "hash.go" ],
                "Imports": [
                    "io"
                ],
                "Deps": [
                    "errors",
                "io",
                "runtime",
                "sync",
                "sync/atomic",
                "unsafe"
                ]
    }
    
  • '-f ' to customize the output format

    $ go list -f '{{join .Deps " "}}' strconv
     errors math runtime unicode/utf8 unsafe
    

    It lists all the dependencies. Actually, the '-f' is like using the text template to format the output.

Section 11, Testing

  • Within *_test.go files, three kinds of functions are treated specially : tests, benchmarks, and examples. Function starts with 'Test' is for tests, 'Benchmark' is for benchmarks, 'Example' is for examples.

Tests

  • '-run' to run specific cases using the regular expression

    $ go test -v -run="French|Canal"
         === RUN TestFrenchPalindrome
         --- FAIL: TestFrenchPalindrome (0.00s)
             word_test.go:28: IsPalindrome("été") = false
         === RUN TestCanalPalindrome
         --- FAIL: TestCanalPalindrome (0.00s)
             word_test.go:35: IsPalindrome("A man, a plan, a canal: Panama") = false
         FAIL
         exit status 1
         FAIL    gopl.io/ch11/word1  0.014s
    

External Test Package

  • Sometimes, in case of a cycle of dependencies in test files, use the _test to declare a external test package. For example, in 'net/url', the test file may declare package like package url_test, the compiler will create an external package for this implicitly.

    $ go list -f={{.GoFiles}} fmt
         [doc.go format.go print.go scan.go]
    $ go list -f={{.TestGoFiles}} fmt
         [export_test.go]
    $ go list -f={{.XTestGoFiles}} fmt
         [fmt_test.go scan_test.go stringer_test.go]
    

    The 'XTestGoFiles' is the external test package files.

  • In external package test file, you cannot access the private variable or functions in the tested package. In order to do while-box testing in external package test files, using a export file to export the private things. And this file is always called export_test.go

    For example, the export_test.go file for the 'fmt' package is:

    package fmt
    var IsSpace = isSpace
    

Benchmark Testing

  • Test and check the memory

    $ go test -bench=. -benchmem
         PASS
              BenchmarkIsPalindrome    2000000    807 ns/op  128 B/op  1 allocs/op
    

Profiling

  • Profiling when testing

    $ go test -cpuprofile=cpu.out
    $ go test -blockprofile=block.out
    $ go test -memprofile=mem.out
    

    And then run the pprof tool

        $ go test -run=NONE -bench=ClientServerParallelTLS64 \
                 -cpuprofile=cpu.log net/http
         PASS
         BenchmarkClientServerParallelTLS64-8  1000
            3141325 ns/op  143010 B/op  1747 allocs/op
         ok      net/http       3.395s
         $ go tool pprof -text -nodecount=10 ./http.test cpu.log
         2570ms of 3590ms total (71.59%)
         Dropped 129 nodes (cum <= 17.95ms)
         Showing top 10 nodes out of 166 (cum >= 60ms)
             flat  flat%   sum%     cum   cum%
           1730ms 48.19% 48.19%  1750ms 48.75%  crypto/elliptic.p256ReduceDegree
            230ms  6.41% 54.60%   250ms  6.96%  crypto/elliptic.p256Diff
            120ms  3.34% 57.94%   120ms  3.34%  math/big.addMulVVW
            110ms  3.06% 61.00%   110ms  3.06%  syscall.Syscall
             90ms  2.51% 63.51%  1130ms 31.48%  crypto/elliptic.p256Square
             70ms  1.95% 65.46%   120ms  3.34%  runtime.scanobject
             60ms  1.67% 67.13%   830ms 23.12%  crypto/elliptic.p256Mul
             60ms  1.67% 68.80%   190ms  5.29%  math/big.nat.montgomery
             50ms  1.39% 70.19%    50ms  1.39%  crypto/elliptic.p256ReduceCarry
             50ms  1.39% 71.59%    60ms  1.67%  crypto/elliptic.p256Sum
    

    The -nodecount=10 means showing only 10 rows

Example Testing

  • For example test ExampleFuncName, the go doc will add this example to the function FuncName automatically.

Section 12, Reflection

reflect, type and value

  • reflect.TypeOf return the dynamic type of the interface value. The '%T' in fmt is using this.

    var w io.Writer = os.Stdout
    fmt.Println(reflect.TypeOf(w)) // "*os.File"
    
  • reflect.ValueOf to get the value

    v := reflect.ValueOf(3) // a reflect.Value
    fmt.Println(v)          // "3"
    fmt.Printf("%v\n", v)   // "3"
    fmt.Println(v.String()) // NOTE: "<int Value>"
    t := v.Type() // a reflect.Type
    fmt.Println(t.String()) // "int"
    v := reflect.ValueOf(3) // a reflect.Value
    x := v.Interface() // an interface{}
    i := x.(int) // an int
    fmt.Printf("%d\n", i) // "3"
    
  • reflect.Value.Kind usage. Kind of zero value is reflect.Invalid

    func formatAtom(v reflect.Value) string {
             switch v.Kind() {
             case reflect.Invalid:
                 return "invalid"
             case reflect.Int, reflect.Int8, reflect.Int16,
                 reflect.Int32, reflect.Int64:
                 return strconv.FormatInt(v.Int(), 10)
             case reflect.Uint, reflect.Uint8, reflect.Uint16,
                 reflect.Uint32, reflect.Uint64, reflect.Uintptr:
                 return strconv.FormatUint(v.Uint(), 10)
             // ...floating-point and complex cases omitted for brevity...
             case reflect.Bool:
                 return strconv.FormatBool(v.Bool())
             case reflect.String:
                 return strconv.Quote(v.String())
             case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Slice, reflect.Map:
                 return v.Type().String() + " 0x" +
                     strconv.FormatUint(uint64(v.Pointer()), 16)
             default: // reflect.Array, reflect.Struct, reflect.Interface
                 return v.Type().String() + " value"
    } }
    
  • reflect for the Composite Types

    func display(path string, v reflect.Value) {
             switch v.Kind() {
             case reflect.Invalid:
                 fmt.Printf("%s = invalid\n", path)
             case reflect.Slice, reflect.Array:
                 for i := 0; i < v.Len(); i++ {
                     display(fmt.Sprintf("%s[%d]", path, i), v.Index(i))
                 }
             case reflect.Struct:
                 for i := 0; i < v.NumField(); i++ {
                     fieldPath := fmt.Sprintf("%s.%s", path, v.Type().Field(i).Name)
                     display(fieldPath, v.Field(i))
                 }
             case reflect.Map:
                 for _, key := range v.MapKeys() {
                     display(fmt.Sprintf("%s[%s]", path,
                         formatAtom(key)), v.MapIndex(key))
                 }
             case reflect.Ptr:
                 if v.IsNil() {
                     fmt.Printf("%s = nil\n", path)
                 } else {
                     display(fmt.Sprintf("(*%s)", path), v.Elem())
                 }
             case reflect.Interface:
                 if v.IsNil() {
                     fmt.Printf("%s = nil\n", path)
                 } else {
                     fmt.Printf("%s.type = %s\n", path, v.Elem().Type())
                     display(path+".value", v.Elem())
                 }
             default: // basic types, channels, funcs
                 fmt.Printf("%s = %s\n", path, formatAtom(v))
             }
    }
    
  • Set value

    x := 1
    rx := reflect.ValueOf(&x).Elem()
    rx.SetInt(2) // OK, x = 2
    rx.Set(reflect.ValueOf(3)) // OK, x = 3
    rx.SetString("hello") // panic: string is not assignable to int
    rx.Set(reflect.ValueOf("hello")) // panic: string is not assignable to int
    var y interface{}
    ry := reflect.ValueOf(&y).Elem()
    ry.SetInt(2) // panic: SetInt called on interface Value
    ry.Set(reflect.ValueOf(3)) // OK, y = int(3)
    ry.SetString("hello") // panic: SetString called on interface Value
    ry.Set(reflect.ValueOf("hello")) // OK, y = "hello"
    
  • We can use reflect to access the unexport fields of a struct, but you cannot update it, because the reflect will records whether it's exported or not.

    stdout := reflect.ValueOf(os.Stdout).Elem() // *os.Stdout, an os.File var
    fmt.Println(stdout.Type())                  // "os.File"
    fd := stdout.FieldByName("fd")
    fmt.Println(fd.Int()) // "1"
    fd.SetInt(2)          // panic: unexported field
    fmt.Println(fd.CanAddr(), fd.CanSet()) // "true false"
    
  • access the struct tags

    // Build map of fields keyed by effective name.
        fields := make(map[string]reflect.Value)
        v := reflect.ValueOf(ptr).Elem() // the struct variable
        for i := 0; i < v.NumField(); i++ {
            fieldInfo := v.Type().Field(i) // a reflect.StructField
            tag := fieldInfo.Tag           // a reflect.StructTag
            name := tag.Get("http")
            if name == "" {
                name = strings.ToLower(fieldInfo.Name)
            }
            fields[name] = v.Field(i)
        }
    
  • access the methods. Using reflect.Value.Call is possible to call the method

    // Print prints the method set of the value x.
     func Print(x interface{}) {
         v := reflect.ValueOf(x)
         t := v.Type()
         fmt.Printf("type %s\n", t)
         for i := 0; i < v.NumMethod(); i++ {
             methType := v.Method(i).Type()
             fmt.Printf("func (%s) %s%s\n", t, t.Method(i).Name,
                 strings.TrimPrefix(methType.String(), "func"))
        }
    }
    
  • Reflect is fragile and will cause terrible performance issue is it's in the critical code path.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瞪慧,一起剝皮案震驚了整個(gè)濱河市急黎,隨后出現(xiàn)的幾起案子髓迎,更是在濱河造成了極大的恐慌悬秉,老刑警劉巖玛荞,帶你破解...
    沈念sama閱讀 206,968評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡撩银,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門鬼佣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)驶拱,“玉大人,你說(shuō)我怎么就攤上這事晶衷±陡伲” “怎么了阴孟?”我有些...
    開封第一講書人閱讀 153,220評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)税迷。 經(jīng)常有香客問(wèn)我永丝,道長(zhǎng),這世上最難降的妖魔是什么箭养? 我笑而不...
    開封第一講書人閱讀 55,416評(píng)論 1 279
  • 正文 為了忘掉前任慕嚷,我火速辦了婚禮,結(jié)果婚禮上毕泌,老公的妹妹穿的比我還像新娘喝检。我一直安慰自己,他們只是感情好撼泛,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,425評(píng)論 5 374
  • 文/花漫 我一把揭開白布挠说。 她就那樣靜靜地躺著,像睡著了一般愿题。 火紅的嫁衣襯著肌膚如雪损俭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,144評(píng)論 1 285
  • 那天潘酗,我揣著相機(jī)與錄音杆兵,去河邊找鬼。 笑死崎脉,一個(gè)胖子當(dāng)著我的面吹牛拧咳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播囚灼,決...
    沈念sama閱讀 38,432評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼骆膝,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了灶体?” 一聲冷哼從身側(cè)響起阅签,我...
    開封第一講書人閱讀 37,088評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蝎抽,沒想到半個(gè)月后政钟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,586評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡樟结,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,028評(píng)論 2 325
  • 正文 我和宋清朗相戀三年养交,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瓢宦。...
    茶點(diǎn)故事閱讀 38,137評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡碎连,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出驮履,到底是詐尸還是另有隱情鱼辙,我是刑警寧澤廉嚼,帶...
    沈念sama閱讀 33,783評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站倒戏,受9級(jí)特大地震影響怠噪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜杜跷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,343評(píng)論 3 307
  • 文/蒙蒙 一傍念、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧葱椭,春花似錦捂寿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至治笨,卻和暖如春驳概,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背旷赖。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工顺又, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人等孵。 一個(gè)月前我還...
    沈念sama閱讀 45,595評(píng)論 2 355
  • 正文 我出身青樓稚照,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親俯萌。 傳聞我的和親對(duì)象是個(gè)殘疾皇子果录,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,901評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容