GitBucket
4.23.0
Toggle navigation
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
sample-golang
/
01_hello
Browse code
add struct sample
master
1 parent
e2301b0
commit
fdd217cb200e3f6bc2289adefcb3eee913f9660a
yhornisse
authored
on 25 Sep 2021
Patch
Showing
4 changed files
struct/anonymousField.go
struct/json.go
struct/struct1.go
struct/struct2.go
Ignore Space
Show notes
View
struct/anonymousField.go
0 → 100644
package main import "fmt" type SampleStruct1 struct { Id int64 Name string } type SampleStruct2 struct { SampleStruct1 Address string } type SampleStruct3 struct { SampleStruct1 // SampleStruct1 // NG! Address string } func main() { v1 := SampleStruct2{SampleStruct1{1, "taro"}, "address"} fmt.Println(v1) // {{1 taro} address} v2 := SampleStruct2{} fmt.Println(v2) // {{0 } } v2.Id = 2 v2.Name = "jiro" v2.Address = "address2" fmt.Println(v2) // {{2 jiro} address2} v3 := SampleStruct2{ SampleStruct1: SampleStruct1{1, "taro"} } fmt.Println(v3) // {{1 taro} } }
Ignore Space
Show notes
View
struct/json.go
0 → 100644
package main import ( "fmt" "encoding/json" ) type SampleStruct1 struct { Id int64 Name string } type SampleStruct2 struct { TestName string `json:"test_name"` } func main() { v1 := SampleStruct1{1, "taro"} if data, err := json.Marshal(v1); err == nil { fmt.Printf("%s\n", data) // {"Id":1,"Name":"taro"} v2 := SampleStruct1{} if err := json.Unmarshal(data, &v2); err == nil { fmt.Println(v2) // {1 taro} } } else { fmt.Println(err) } v3 := SampleStruct2{} if err := json.Unmarshal([]byte("{ \"test_name\": \"hoge-\" }"), &v3); err == nil { fmt.Println(v3) // {hoge-} } else { fmt.Println(err) } }
Ignore Space
Show notes
View
struct/struct1.go
0 → 100644
package main import "fmt" type SampleStruct struct { Id int64 Name string } func main() { v1 := SampleStruct{1, "taro"} fmt.Println(v1) // {Id:1 Name:taro} hoge(v1) // 値渡し fmt.Println(v1) // {Id:1 Name:taro} v2 := SampleStruct{ Name: "jiro" } fmt.Println(v2) // {0 jiro} } func hoge(v SampleStruct) { fmt.Println(v) // {Id:1 Name:taro} v.Id = 2 fmt.Println(v) // {Id:2 Name:taro} }
Ignore Space
Show notes
View
struct/struct2.go
0 → 100644
package main import "fmt" type SampleStruct struct { Id int64 Name, Address string } func main() { v1 := SampleStruct{1, "taro", "address"} fmt.Println(v1) // {Id:1 Name:taro Address:address} v2 := SampleStruct{1, "taro", "address"} v3 := SampleStruct{1, "jiro", "address"} fmt.Println(v1 == v2) // true fmt.Println(v1 == v3) // false p1 := &v1 fmt.Printf("%p\n", p1) (*p1).Id = 2 fmt.Println(*p1) // {Id:2 Name:taro Address:address} fmt.Println(v1) // {Id:2 Name:taro Address:address} }
Show line notes below