This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mutex sync.Mutex | |
type Person struct { | |
Age int | |
} | |
persons := make([]Person, 10) | |
for _, p := range persons { | |
mutex.Lock() | |
// defer mutex.Unlock() | |
p.Age = 13 | |
mutex.Unlock() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mutex sync.Mutex | |
type Person struct { | |
Age int | |
} | |
persons := make([]Person, 10) | |
for _, p := range persons { | |
func() { | |
mutex.Lock() | |
defer mutex.Unlock() | |
p.Age = 13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func doReq(timeout time.Duration) obj { | |
// ch :=make(chan obj) | |
ch := make(chan obj, 1) | |
go func() { | |
obj := do() | |
ch <- result | |
} () | |
select { | |
case result = <- ch : | |
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BadOrderedPerson struct { | |
Veteran bool // 1 byte | |
Name string // 16 byte | |
Age int32 // 4 byte | |
} | |
type OrderedPerson struct { | |
Name string | |
Age int32 | |
Veteran bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type BadOrderedPerson struct { | |
Veteran bool // 1 byte | |
_ [7]byte // 7 byte: padding for alignment | |
Name string // 16 byte | |
Age int32 // 4 byte | |
_ struct{} // to prevent unkeyed literals | |
// zero sized values, like struct{} and [0]byte occurring at | |
// the end of a structure are assumed to have a size of one byte. | |
// so padding also will be addedd here as well. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func init() { | |
http.HandleFunc("/debug/pprof/", Index) | |
http.HandleFunc("/debug/pprof/cmdline", Cmdline) | |
http.HandleFunc("/debug/pprof/profile", Profile) | |
http.HandleFunc("/debug/pprof/symbol", Symbol) | |
http.HandleFunc("/debug/pprof/trace", Trace) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func init() { | |
go func() { | |
http.ListenAndServe(":1234", nil) | |
}() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
defer profile.Start(profile.ProfilePath(".")).Stop() | |
// do something | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CPUProfile enables cpu profiling. Note: Default is CPU | |
defer profile.Start(profile.CPUProfile).Stop() | |
// GoroutineProfile enables goroutine profiling. | |
// It returns all Goroutines alive when defer occurs. | |
defer profile.Start(profile.GoroutineProfile).Stop() | |
// BlockProfile enables block (contention) profiling. | |
defer profile.Start(profile.BlockProfile).Stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"encoding/json" | |
"math/rand" | |
"net/http" | |
_ "net/http/pprof" | |
"time" | |
) | |
func main() { | |
http.HandleFunc("/log", logHandler) |