@ -0,0 +1,4 @@ | |||||
# 打印堆栈 | |||||
fmt.Printf("%s", debug.Stack()) | |||||
debug.PrintStack() | |||||
可以通过 debug.PrintStack() 直接打印,也可以通过 debug.Stack() 方法获取堆栈然后自己打印。 |
@ -0,0 +1,33 @@ | |||||
package main | |||||
import ( | |||||
"fmt" | |||||
"math" | |||||
) | |||||
var XX = 200 | |||||
const YY = 300 //常量不会分配地址 | |||||
func main() { | |||||
const ( | |||||
x uint16 = 120 | |||||
y | |||||
s | |||||
s1 = "abc" | |||||
z | |||||
) | |||||
println(x, " ", y, " ", s, " ", s1, " ", z) | |||||
const ( | |||||
a = iota | |||||
a1 | |||||
b float32 = iota | |||||
c | |||||
) | |||||
println(a, " ", a1, " ", b, " ", c) | |||||
// println(&XX, &YY) // error | |||||
fmt.Printf("%v %v \n", math.MaxUint8, math.MaxInt64) | |||||
} |
@ -0,0 +1,39 @@ | |||||
package main | |||||
func tets() (a int, s string, e error) { | |||||
return 0, "", nil | |||||
} | |||||
func test(f func()) { | |||||
f() | |||||
} | |||||
func makeFun() func(int, int) int { | |||||
return func(x, y int) int { | |||||
return x - y | |||||
} | |||||
} | |||||
func main() { | |||||
tets() | |||||
// 直接调用匿名函数 | |||||
func(s string) { | |||||
println("the string is ", s) | |||||
}("hello world") | |||||
// 将匿名函数复制给变量 | |||||
add := func(x, y int) int { | |||||
return x + y | |||||
} | |||||
println("the add ret is ", add(1, 2)) | |||||
// 作为参数 | |||||
test(func() { println("this is one func") }) | |||||
// 作为返回值 | |||||
sub := makeFun() | |||||
println("the sub fun retrun is ", sub(10, 1)) | |||||
} |
@ -0,0 +1,37 @@ | |||||
package main | |||||
import ( | |||||
"os" | |||||
"os/signal" | |||||
"syscall" | |||||
) | |||||
func main() { | |||||
ch := make(chan os.Signal, 1) | |||||
//signal.Notify(ch, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGSTOP, syscall.SIGUSR1) | |||||
signal.Notify(ch) | |||||
for { | |||||
sig := <-ch | |||||
switch sig { | |||||
case syscall.SIGHUP: | |||||
println("SIGSTOP") | |||||
return | |||||
case syscall.SIGQUIT: | |||||
println("SIGQUIT") | |||||
return | |||||
case syscall.SIGTERM: | |||||
println("SIGTERM") | |||||
return | |||||
case syscall.SIGSTOP: | |||||
println("SIGSTOP") | |||||
return | |||||
case syscall.SIGUSR1: | |||||
println("SIGUSR1") | |||||
return | |||||
default: | |||||
println("what is this ", sig) | |||||
return | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,9 @@ | |||||
package main | |||||
import "os" | |||||
func main() { | |||||
hostname, err := os.Hostname() | |||||
println(hostname, " ", err) | |||||
} |
@ -0,0 +1,41 @@ | |||||
package main | |||||
import ( | |||||
"os" | |||||
"strconv" | |||||
"time" | |||||
) | |||||
var share int = 0 | |||||
var Cnt int | |||||
func GSet(index int, ch chan struct{}) { | |||||
for i := 0; i < Cnt; i++ { | |||||
//println("cur index ", index, "cur vale", share) | |||||
share += 1 | |||||
ch <- struct{}{} | |||||
} | |||||
} | |||||
func main() { | |||||
Num, _ := strconv.Atoi(os.Args[1]) | |||||
Cnt, _ = strconv.Atoi(os.Args[2]) | |||||
println(Num, Cnt) | |||||
ch := make(chan struct{}) | |||||
start := time.Now() | |||||
for i := 0; i < Num; i++ { | |||||
go GSet(i, ch) | |||||
} | |||||
GetNum := 0 | |||||
for { | |||||
<-ch | |||||
GetNum++ | |||||
//println("add one num ", GetNum) | |||||
if GetNum >= Cnt*Num { | |||||
break | |||||
} | |||||
} | |||||
use := time.Since(start) | |||||
println("test over ", share, "should be ", Cnt*Num, "use time ", use) | |||||
} |