This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
// 基本的な型である。単一条件 | |
i := 1 | |
for i <= 3 { | |
fmt.Println(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
//例です。 | |
if 7%2 == 0 { | |
fmt.Println("7 is even") | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
// 5つのintを持つarrayを作ります。 | |
// 要素の型と長さはarrayの型のである。 | |
// 最初からarrayはzero valueである。intsの場合は0 | |
var a [5]int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
import "time" | |
func main() { | |
// 基本的な例 | |
i := 2 | |
fmt.Print("write ", i, " as ") | |
switch i { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
// arraysとは違い、slicesは中に入っている要素の型のみで定義されます。(要素の数は含まれない) | |
// 長さがゼロではないからのsliceを作るには、buildinのmakeを使用する。 | |
// これは、長さが3のstringのsliceを作る例です。(値はzero valueで初期化されている。) | |
s := make([]string, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
// 空のmapを作るためにbuildinのmakeを使います。 | |
// make(map[key-type]val-type) | |
m := make(map[string]int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
// ここではsliceの中の数字を合計するためにrangeを使います。 | |
// arrayでも同じように動きます。 | |
nums := []int{2, 3, 4} | |
sum := 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// 2つのintを取りその合計を返す関数です。 | |
func plus(a int, b int) int { | |
// Goは明示的なreturnが必要です。 | |
// 例えば、最後の値を自動的に返すことはありません。 | |
return a + b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
//この関数において(int, int)はこの関数は2つのintを返すことを示します。 | |
func vals() (int, int) { | |
return 3, 7 | |
} | |
func main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// この関数は任意の数のintを引数として受け入れます。 | |
func sum(nums ...int) { | |
fmt.Print(nums, " ") | |
total := 0 | |
for _, num := range nums { | |
total += num | |
} |