LoginSignup
6
6

More than 5 years have passed since last update.

GoをMacにインストールしてFizzBuzzをやってみた

Posted at

最近巷で人気なGolang。
ちょっと興味出たのでやってみようかなと思って手持ちのMacにインストールしてHelloしたりFizzbuzz作ったりしてみた。

インストール

Homebrew使ってるのでコマンド一発。

brew install goでインストールは完了。
あとはターミナル内で使うのでPathを通す必要がある。

$ echo 'export GOPATH= "/path/to/go/bin"' >> ~/.zshrc
$ echo 'export PATH="$PATH:$GOPATH"' >> ~/.zshrc
$ source ~/.zshrc
$ go
Go is a tool for managing Go source code.

Usage:

    go command [arguments]

The commands are:

    build       compile packages and dependencies
    clean       remove object files
    env         print Go environment information
    fix         run go tool fix on packages
    fmt         run gofmt on package sources
    get         download and install packages and dependencies
    install     compile and install packages and dependencies
    list        list packages
    run         compile and run Go program
    test        test packages
    tool        run specified go tool
    version     print Go version
    vet         run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

    c           calling between Go and C
    filetype    file types
    gopath      GOPATH environment variable
    importpath  import path syntax
    packages    description of package lists
    testflag    description of testing flags
    testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.

これでインストールは完了。

Hello Go.

拡張子はhoge.goになる。簡単で分かりやすい。
文法とかはJavaとかPythonに近い。…様な気がする。

hello.go
package main

import "fmt"

func main() {
  fmt.Println("Hello go language.")
}

Fizzbuzz

PGなら大体書いた事があるらしいFizzbuzz。
折角なので2つ書いてみた。

fizzbuzz1.go
package main

import (
  "fmt"
  "strconv"
  )

func fizzbuzz(num int) string {
  for i := 0; i <= num;i++ {
    fizz := i % 3
    buzz := i % 5
    switch {
    case fizz==0 && buzz ==0:
      fmt.Println("fizzbuzz")
    case fizz==0 && buzz !=0:
      fmt.Println("fizz")
    case fizz!=0 && buzz ==0:
      fmt.Println("buzz")
    default:
      fmt.Println(strconv.Itoa(i))
    }
  }
  return "\nEnd"
}

func main() {
  fmt.Println(fizzbuzz(100))
}
fizzbuzz2.go
package main

import (
  "fmt"
  "strconv"
  )

func execute(num int) string {
  fizz := num % 3
  buzz := num % 5
  if fizz==0 && buzz==0 {
    return "fizzbuzz"
  } else if fizz!=0 && buzz==0 {
    return "buzz"
  } else if fizz==0 && buzz!=0 {
    return "fizz"
  } else {
    return strconv.Itoa(num)
  }
}

func main() {
  for i:=0;i<100;i++ {
    fmt.Println(execute(i))
  }
}

再起使った書き方とかは今度やろうかなと思う。

感想

書き方はちょい癖とかあるけど覚えれば結構書きやすいし
動作も軽めだし今後も利用頻度増えそうな良い言語かなぁと思う。

参考

http://go-tour-jp.appspot.com/
http://golang.jp/
http://qiita.com/tenntenn/items/0e33a4959250d1a55045
golang.jpのとこに書いてある言語仕様のページは最低限な知識を得られるので必読だと思う。

6
6
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
6
6