Skip to content

Instantly share code, notes, and snippets.

@chenhengqi
Last active December 24, 2020 09:30
Show Gist options
  • Save chenhengqi/ba51affc7154e37ebc2259ac22a8f304 to your computer and use it in GitHub Desktop.
Save chenhengqi/ba51affc7154e37ebc2259ac22a8f304 to your computer and use it in GitHub Desktop.
Dive into Go

Code Snippet

package main

import (
        "fmt"
        "math/rand"
        "time"
)

func computeE(r int32) float64 {
        e := 2.0
        factorial := 1.0

        for i := int32(2); i < r; i++ {
                factorial *= float64(i)
                e += 1 / factorial
        }
        return e
}

func main() {
        for {
                r := rand.Int31() % 10000
                e := computeE(r)
                fmt.Println(r, e)
                time.Sleep(time.Second)
        }
}

Symbols

$ go tool objdump -s main.computeE app
$ objdump --sym app

Assembly

$ go tool compile -S main.go
$ objdump -d app

Debug

$ dlv exec ./app
(dlv) break main.go:13
(dlv) break main.computeE
(dlv) goroutines -tf
(dlv) goroutine ${id}
(dlv) frame ${level}
(dlv) regs -a
(dlv) disassemble -l main.go:13

Tracing

$ strace -p $PID
$ gstack -p $PID
$ pstack -p $PID
@chenhengqi
Copy link
Author

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