Help us understand the problem. What is going on with this article?

ずんだのハロウィン問題 Go編

More than 1 year has passed since last update.

ずんだのハロウィン問題 Ruby編
https://qiita.com/tbpgr/items/1f2ed0e14e5d94775963

ルール

  • 入力として "trick", "treat", "sushi", "poyayou", "tamakatsu" がランダムに与えられる
  • 入力をそのまま標準出力する
  • 入力が "trick" または "treat" だったら "happy halloween" と標準出力する
  • 入力が "trick" または "treat" ではなかったら "sleepy" と標準出力する

プログラム(Go)

zunda.go

package main

import (
    "fmt"
    "io"
    "os"
)

var writer io.Writer = os.Stdout

func main() {
    var s string
    fmt.Scan(&s)
    Halloween(s)
}

func Halloween(s string) {
    fmt.Println(s)
    switch s {
    case "trick", "treat":
        fmt.Fprint(writer, "happy halloween")
    default:
        fmt.Fprint(writer, "sleepy")
    }
}

zunda_test.go

package main

import (
    "bytes"
    "testing"
)

var buffer *bytes.Buffer

func TestHalloween(t *testing.T) {
    buffer = &bytes.Buffer{}
    writer = buffer

    Halloween("trick")
    if buffer.String() != "happy halloween" {
        t.Fail()
    }

    buffer.Reset()
    Halloween("treat")
    if buffer.String() != "happy halloween" {
        t.Fail()
    }

    buffer.Reset()
    Halloween("sushi")
    if buffer.String() != "sleepy" {
        t.Fail()
    }

    buffer.Reset()
    Halloween("poyayou")
    if buffer.String() != "sleepy" {
        t.Fail()
    }

    buffer.Reset()
    Halloween("tamakatsu")
    if buffer.String() != "sleepy" {
        t.Fail()
    }
}

出力例

$ go run main.go
trick
happy halloween

$ go run main.go
poyayou
sleepy

$ go test -v
=== RUN   TestHalloween
trick
treat
sushi
poyayou
tamakatsu
--- PASS: TestHalloween (0.00s)
PASS
ok      _/home/yukpiz/labo/temp/zunda   0.002s
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away