Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@yukpiz

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

ずんだのハロウィン問題 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
0
Help us understand the problem. What are the problem?