19
15

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

Go: 計算なしのFizzBuzz

Posted at

割り算などの計算をせず、時に身を任せるFizzBuzzです。絶妙なバランスでなりたっています。

package main

import (
	"time"
)

func main() {
	i := 1
	skip := false

	fizz := time.Tick(3 * time.Second) // 3秒ごとに送信するチャネル
	time.Sleep(10 * time.Millisecond)  // 前後のチャネルの開始時刻を微調整
	buzz := time.Tick(5 * time.Second) // 5秒ごとに送信するチャネル
	time.Sleep(10 * time.Millisecond)  // 前後のチャネルの開始時刻を微調整
	num := time.Tick(1 * time.Second)  // 1秒ごとに送信するチャネル

	for {
		select {
		case <-fizz:
			print("Fizz")
			skip = true
		case <-buzz:
			print("Buzz")
			skip = true
		case <-num:
			if !skip {
				print(i)
			}
			print("\n")
			skip = false
			i++
		}

		if i > 100 {
			return
		}
	}
}

このコードを実行してみる

19
15
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
19
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?