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 3 years have passed since last update.

golang やってみた mainに引数を入れつつ go routineを入れてみた

Posted at

mainにコマンドライン実行時に引数を与えるには、flagを使う。
何も指定しないと、Stringの配列で取得されるので、数字をとるなら、strconvで型変換が必要だった。
引数に、引数名を指定できるので、その場合は型も指定できる。

go routineで、先に終わったものをselectで取得するようにしてみた。
単純な計算なので、先に始めた方が先に終わるのかな。
go routineの前後を入れ替えると、先に終わるのが変わる。

package main                                                                    

import (
    "flag"
    "fmt"
    "strconv"
)

func main() {
    flag.Parse()
    x_arg, _ := strconv.Atoi(flag.Arg(0))
    chan_x := make(chan int)
    chan_y := make(chan int)
    go func() { chan_x <- 4 + 4 }() 
    go func(t int) { chan_y <- t * t }(x_arg)
    var x int 
    var y int 
    select {
    case x = <-chan_x:
        fmt.Println(x)
    case y = <-chan_y:
        fmt.Println(y)
    }   
    return

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?