Go struct
Go does not have class, struct plays similar role as class in other languages
Go struct definition
type ABC struct { name string number int }
Go struct init
a := ABC{} a.name = "Nyannyan" a.number = 1 c := ABC{ "Mon", 3, } d := new(ABC) d.name = "Sansan" d.number = 4 p := &ABC{"Pin", 5}
Go struct embed
Struct in struct. It’s like extend
type ABC struct { name string number int } type ExABC struct { ext int ABC } ext := ExABC{1, ABC{"Boo", 2}}
Full example
package main import ( "fmt" ) type ABC struct { name string number int } type ExABC struct { ext int ABC } func main() { a := ABC{} a.name = "Nyannyan" a.number = 1 fmt.Println(a.name) c := ABC{ "Mon", 3, } d := new(ABC) d.name = "Sansan" d.number = 4 p := &ABC{"Pin", 5} fmt.Println(a) fmt.Println(c) fmt.Println(d) fmt.Println(p) ext := ExABC{1, ABC{"Boo", 2}} fmt.Println(ext) var instruct struct { name string number int } instruct.name = "Gok" instruct.number = 5 fmt.Println(instruct) }
コメント