/** * Gentle intro to Go structs * structs are like Java classes, but without methods and without inheritance. * (mostly). *@author gtowell * Created: Sep 12, 2021 */ package main import "fmt" // create a basic struct. // It holds 3 things. type Strc struct { A int b string c float64 } // this behaves much the same as the toString method in Java func (item Strc) String() string { return fmt.Sprintf("Strcc A:%v b:%v c:%v", item.A, item.b, item.c); } type AStrc Strc func main() { aa := AStrc{2,"sss", 3.1415} // fill in order bb := Strc{b:"ggg", A:7, c:1.414} //fill by name // printing structs is easy. fmt.Printf("%v\n%+v\n", aa, bb) // q := aa.A * aa.c // NOT ALLOWED -- if number are not the same type you must cast qi := aa.A * int(bb.c) qf := float64(aa.A) * bb.c fmt.Printf("int:%d float:%f\n", qi, qf) a2 := aa fmt.Printf("A2:%v, aa==a2:%t\n", a2, aa==a2) a2.c = 55.4 fmt.Printf("A2:%v, aa==a2:%t\n", a2, aa==a2) //a3 := Strc{a2.A, a2.b, a2.c} //fmt.Printf("A3:%v, aa==a3:%t\n", a3, a2==a3) //this line will not compile b2 := AStrc{a2.A, a2.b, a2.c} fmt.Printf("b2:%v\n", b2) //fmt.Printf("b2:%v, aa==b2:%t\n", b2, aa==b2) // compiler flags this out to not allow compare of mismatched types. .. Why not allow? }