Created
January 27, 2016 16:44
-
-
Save motokiee/b26fe336051556835899 to your computer and use it in GitHub Desktop.
ちょっとgolangいじったし寝る #CodePiece
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" | |
type Person struct { | |
*Programmer | |
name string | |
age int | |
} | |
type Programmer struct { | |
language string | |
} | |
func newProgrammer(language string) *Programmer { | |
return &Programmer{language} | |
} | |
func newPerson(name string, age int, language string) *Person { | |
return &Person{newProgrammer(language), name, age} | |
} | |
func (p Programmer) coding() { | |
fmt.Printf("Hi, I'm %s programmer.", p.language) | |
} | |
func (p Person) greet() { | |
fmt.Printf("Hi, %s\n", p.name) | |
} | |
func main() { | |
person := newPerson("Motoki", 30, "Swift") | |
person.greet() | |
person.coding() | |
// runeはUnicodeそのものを表す | |
r := []rune("日本語") | |
fmt.Printf("%#U\n", r[0]) | |
// 配列 | |
var array1 [5]int | |
fmt.Println(array1) | |
array1[2] = 5 | |
fmt.Println(array1) | |
array2 := [3]string{"a", "b", "c"} | |
fmt.Println(array2) | |
array3 := [...]string{"サイズ", "を", "省略", "できる"} | |
fmt.Println(array3) | |
fmt.Println(len(array3)) // 配列の長さ | |
// map==連想配列 | |
var map1 map[string]int = map[string]int{} | |
map1["one"] = 1 | |
map1["two"] = 2 | |
fmt.Println(map1) | |
map2 := map[int]bool{1: true, 2: false} | |
fmt.Println(map2) | |
fmt.Println(map2[2]) | |
a := [...]int{1, 2, 3, 4} | |
fmt.Printf("%#v\n", a) | |
// 配列からスライス | |
s := a[:] | |
fmt.Printf("%#v\n", s) | |
// リテラルからスライス | |
s2 := []string{"a", "b", "c"} | |
fmt.Printf("%#v\n", s2) | |
// makeを使ったスライス | |
s3 := make([]bool, 2, 5) | |
fmt.Printf("%#v\n", s3) | |
fmt.Println(len(s3)) | |
fmt.Println(cap(s3)) | |
// スライスのコピー | |
sl1 := []int{1, 2, 3, 4, 5} | |
sl2 := make([]int, 2) | |
copy(sl2, sl1[1:3]) | |
fmt.Printf("%#v\n", sl2) | |
sl2[0] = 5 | |
fmt.Printf("%#v\n", sl2) | |
// const | |
const ( | |
One = 1 | |
Two = 2 | |
Three = 3 | |
) | |
fmt.Println(One) | |
fmt.Println(Two) | |
fmt.Println(Three) | |
if One == 1 { | |
fmt.Println("One is 1") | |
} else { | |
fmt.Println("One is not 1") | |
} | |
if twice := One * 2; twice == 3 { | |
fmt.Println("One * 2 = 3") | |
} else if twice == 2 { | |
fmt.Println("One * 2 = 2") | |
} else { | |
fmt.Println("other") | |
} | |
for_array := [...]int{9999, 14514, 134, 123, 41} | |
for i, element := range for_array { | |
fmt.Printf("%d: %d\n", i, element) | |
} | |
japanese := "日本語" | |
for i, element := range japanese { | |
fmt.Printf("%d: %#U\n", i, element) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment