Go语言中文网 为您找到相关结果 4

Golang 简单的启用一个线程

// code by shaoyongyang package main import ( "fmt" "time" ) func say(s string) { fmt.Println(s) } func main() { go say("who are you?") go say("who are you?1") fmt.Println(2) time.Sleep(1e9) } 好吧,我之前都在想为什么say方法里面没有任何输出,查了很多资料后发现,是因为程序已经结束运行了,启动的线程还在运行。 在最后面加入线程休眠时间就可以了。 启用一个线程仅仅 go say("see i am new thread") ,仅仅使用go关键字即...阅读全文

博文 2015-02-01 08:00:01 未来还没来

golang sleep

版权声明:本文为翔云原创文章,未经博主允许不得转载。 https://blog.csdn.net/lanyang123456/article/details/78158457 golang的休眠可以使用time包中的sleep。 函数原型为: func Sleep(d Duration) 其中的Duration定义为: type Duration int64 Duration的单位为 nanosecond。 为了便于使用,time中定义了时间常量: const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Second = 1000 * Millisecon...阅读全文

博文 2019-03-04 15:31:36 lanyang123456

golang中时间(time)的相关操作

获取当前时间 func getNow() { // 获取当前时间,返回time.Time对象 fmt.Println(time.Now()) // output: 2016-07-27 08:57:46.53277327 +0800 CST // 其中CST可视为美国,澳大利亚,古巴或中国的标准时间 // +0800表示比UTC时间快8个小时 // 获取当前时间戳 fmt.Println(time.Now().Unix()) // 精确到纳秒,通过纳秒就可以计算出毫秒和微妙 fmt.Println(time.Now().UnixNano()) // output: // 1469581066 // 1469581438172080471 } 格式化时间显示 func formatUnixTi...阅读全文

博文 2020-01-04 05:32:39 灰侠_bf44

golang context提前退出

golang中context包实现提前退出 以前不知道怎么写的,一直无法退出,还以为程序就是无法提前退出。。 下面的程序,request休眠100s,然后在另外一个goroutine中,3s后退出所有context import ( "context" "log" "sync" "time" ) func request() { time.Sleep(100 * time.Second) } var wg sync.WaitGroup func do(ctx context.Context,wg *sync.WaitGroup, f func()) { defer wg.Done() ctx2, cancel2 := context.WithCancel(ctx) defer cancel2...阅读全文

博文 2020-04-04 14:32:50 darcyaf