// Structs in go do not have inheritance likes classes // however you can embed one struct inside another so that // during use, the parts of the embedded struct behave exactly // as parts of the enclosing struct. That said, at creation // do do have to to extra work. // Created July 2021 gtowell // Modified Sep 2022 gtowell package main import "fmt" // a simple struct type s1 struct { a int b int } // the struct s1 is in s2, but must be referred to through c type s2 struct { c s1 d int } // s1 is embedded in s3. It does not have an independent name // This may cause some confusion if s1 sand s3 have elements of the // same name but a different type type s3 struct { s1 a float32 d int } func s3make(a, b, c int) s3 { return s3{s1:s1{a:a, b:b}, d:c, a:float32(a*2)} } func main() { as1 := s1{a:2, b:3} fmt.Println(as1) var as2 s2 as2 = s2{c:s1{a:2, b:3}, d:5} fmt.Println(as2) fmt.Println(as2.c.a) as3 := s3make(4, 5, 7) fmt.Printf("%+v\n", as3); fmt.Printf("as3.a: %f\n", as3.a); // get to the embedded a through name of its struct fmt.Printf("as3.s1.a: %d\n", as3.s1.a); }