0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Go言語  — go文を書かなくても goroutine は走っている

Last updated at Posted at 2018-07-28

go func の前に goroutine の数を出力すると 1 が返る。
(既にgoroutineが走っている)

package main

import "fmt"
import "runtime"
import "time"

func main() {
	// Print goroutine num
	fmt.Println(runtime.NumGoroutine()) // 1

	// run goroutine
	go func() {}()

	fmt.Println(runtime.NumGoroutine()) // 2

	time.Sleep(time.Second)
}

理解

  • goroutine とは元々 main の処理にも使われているものであり、必ず1個は走っている。
  • go 文で別の goroutine を走らせることが出来る。

ということだと思う。

きっかけ

A Tour of Go のこの文が気になって調べた。

The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine.

環境

  • go version go1.10.3 darwin/amd64

Gist

参考

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?