fmt.Println("\n\nTest for accessing to elements of struct:") emp1 :=Employee{firstName: "John", secondName: "James"}var emp2 Employee fmt.Println("Employee 3:", emp1, "Employee 4:", emp2) fmt.Println("Now assign to emp3 and emp4") emp2.firstName ="Mike" emp2.secondName ="Don" emp2.age =23 emp2.salary =2000 emp1.firstName ="Test" emp1.age =30 emp1.salary =30000 fmt.Println("Employee 3:", emp1, "Employee 4:", emp2)// Test for accessing to elements of struct:// Employee 3: {John James 0 0} Employee 4: { 0 0}// Now assign to emp3 and emp4// Employee 3: {Test James 30 30000} Employee 4: {Mike Don 23 2000}
packagemainimport ( "fmt")typeAddressstruct { city, state string}typePersonstruct { name string age int address Address}funcmain() { var p Person p.name ="Naveen" p.age =50 p.address = Address { city: "Chicago", state: "Illinois", } fmt.Println("Name:", p.name) fmt.Println("Age:",p.age) fmt.Println("City:",p.address.city) fmt.Println("State:",p.address.state)}
typePeoplestruct { name string age intAddress// Promoted field, can be access by variable directly}typeAddressstruct { city, street string}var p Peoplep.name ="Jackon"p.age =30p.Address = Address { city: "Beijing", street: "二环",}fmt.Println(p, p.Address.city, p.street)