Go 零基础 30 min 入门
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.12.12.linux-amd64.tar.gz
sudo vi /etc/profile
Shift + G
i
export PATH=$PATH:/usr/local/go/bin
~
wq!
source /etc/profile
sudo dpkg -i code_1.39.2-1571154070_amd64.deb
package arithmetic
import "errors"
// Div 除法操作
func Div(a, b int) (c int, err error) {
if b == 0 {
err = errors.New("divisor is 0")
return
}
c = a / b
return
}
package main
import (
"fmt"
"code/arithmetic"
)
func main() {
println("Hello, 世界")
c, err := arithmetic.Div(1, 0)
fmt.Printf("c = %d, err = %+v\n", c, err)
}
go mod init code
go build
./code
package arithmetic
import (
"testing"
"math/rand"
)
func TestDiv(t *testing.T) {
var (
a, b, c int
err error
)
a, b = 1, 2
c, err = Div(a, b)
t.Logf("a = %d, b = %d, c = %d, err = %+v", a, b, c, err)
a, b = 1, 0
c, err = Div(a, b)
t.Logf("a = %d, b = %d, c = %d, err = %+v", a, b, c, err)
}
func BenchmarkDiv(b *testing.B) {
for i := 0; i < b.N; i++ {
Div(rand.Int(), rand.Int())
}
}
package main
import (
"fmt"
"log"
"time"
"net/http"
_ "net/http/pprof"
"code/arithmetic"
)
func main() {
println("Hello, 世界")
c, err := arithmetic.Div(1, 0)
fmt.Printf("c = %d, err = %+v\n", c, err)
for i := 0; i < 1000000; i++ {
go func() {
time.Sleep(time.Second * 10)
}()
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})
err = http.ListenAndServe(":8088", nil)
if err != nil {
log.Fatalf("ListenAndServe: %+v\n", err)
}
}
go pprof debug http://127.0.0.1:8088/debug/pprof/
go tool pprof http://127.0.0.1:8088/debug/pprof/profile -seconds 10
go tool pprof -http=:8081 /home/zhi/pprof/pprof.code.samples.cpu.001.pb.gz