<\/span><\/h1>\n\n\n\nDeclaring a struct just declares a named struct type. Creating a struct variable creates an instance of that struct with memory being initialized as well. We can create a empty struct variable without given any value to any of the field<\/p>\n\n\n\n
emp := employee{}<\/code><\/pre>\n\n\n\nIn this case, all the fields in the struct are initialized with a default zero value of that field type.<\/p>\n\n\n\n
We can also initialize the value for each struct field while creating a struct variable. There are two variations<\/p>\n\n\n\n
- Each field on the same line<\/li><\/ul>\n\n\n\n
emp := employee{name: \"Sam\", age: 31, salary: 2000}<\/code><\/pre>\n\n\n\n- Each field on different lines<\/li><\/ul>\n\n\n\n
emp := employee{\n name: \"Sam\",\n age: 31,\n salary: 2000,\n}<\/code><\/pre>\n\n\n\nIt is also ok to initialize only some of the fields with value. The field which are not initialized with value will get the default zero value of their type<\/p>\n\n\n\n
emp := employee{\n name: \"Sam\",\n age: 31,\n}<\/code><\/pre>\n\n\n\nIn above case salary will get default value of zero since it is not initialized<\/p>\n\n\n\n
Let’s see a working code illustrating above points:<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\ntype employee struct {\n name string\n age int\n salary int\n}\n\nfunc main() {\n emp1 := employee{}\n fmt.Printf(\"Emp1: %+v\\n\", emp1)\n\n emp2 := employee{name: \"Sam\", age: 31, salary: 2000}\n fmt.Printf(\"Emp2: %+v\\n\", emp2)\n\n emp3 := employee{\n name: \"Sam\",\n age: 31,\n salary: 2000,\n }\n fmt.Printf(\"Emp3: %+v\\n\", emp3)\n\n emp4 := employee{\n name: \"Sam\",\n age: 31,\n }\n fmt.Printf(\"Emp4: %+v\\n\", emp4)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEmp1: {name: age:0 salary:0}\nEmp2: {name:Sam age:31 salary:2000}\nEmp3: {name:Sam age:31 salary:2000}\nEmp4: {name:Sam age:31 salary:0}<\/code><\/pre>\n\n\n\nFor above program<\/p>\n\n\n\n
- We first declare an employee<\/strong> struct.<\/li><\/ul>\n\n\n\n
- emp1’s fields are all initialized with default zero value of its type i.e name with “”, age and salary with 0.<\/li><\/ul>\n\n\n\n
- emp2 has been initialized with all fields on the same line. Its fields are correctly printed with their value<\/li><\/ul>\n\n\n\n
- emp3’s has been initialized with all fields on different lines. Its fields are correctly printed with their value<\/li><\/ul>\n\n\n\n
- emp4’s salary field is initialized with default zero value of 0. While other other two fields are correctly printed with their value.<\/li><\/ul>\n\n\n\n
It is to be noted that in the initialization of a struct, every new line with in curly braces has to end with a comma. So below initialization will raise error as<\/p>\n\n\n\n
\"salary\" : 2000<\/code><\/pre>\n\n\n\ndoesn’t end with a comma.<\/p>\n\n\n\n
emp := employee{\n name: \"Sam\",\n age: 31,\n salary: 2000\n}<\/code><\/pre>\n\n\n\nThis will be fine<\/p>\n\n\n\n
emp := employee{\n name: \"Sam\",\n age: 31,\n salary: 2000}<\/code><\/pre>\n\n\n\nWithout field names<\/strong><\/p>\n\n\n\nstruct can also be initialized without specifying the field names. But in this case, all values for each of the field has to be provided in sequence<\/p>\n\n\n\n
emp := employee{\"Sam\", 31, 2000}<\/code><\/pre>\n\n\n\nA compiler error will be raised if all values are not provided when field name is not used.<\/p>\n\n\n\n
Let’s see a program<\/p>\n\n\n\n
package main\n\nimport \"fmt\"\n\ntype employee struct {\n name string\n age int\n salary int\n}\n\nfunc main() {\n emp := employee{\"Sam\", 31, 2000}\n fmt.Printf(\"Emp: %+v\\n\", emp)\n\n \/\/emp = employee{\"Sam\", 31}\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEmp2: {name:Sam age:31 salary:2000}<\/code><\/pre>\n\n\n\nUncomment the line<\/p>\n\n\n\n
emp = employee{\"Sam\", 31}<\/code><\/pre>\n\n\n\nin the above program, and it will raise compiler error<\/p>\n\n\n\n
too few values in employee literal<\/code><\/pre>\n\n\n\n<\/span>Accessing and Setting Struct Fields<\/strong><\/span><\/h1>\n\n\n\nStructs fields can be accessed using the dot operator. Below is the format for getting the value<\/p>\n\n\n\n
n := emp.name<\/code><\/pre>\n\n\n\nSimilarly a value can be assigned to a struct field too.<\/p>\n\n\n\n
emp.name = \"some_new_name\"<\/code><\/pre>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype employee struct {\n name string\n age int\n salary int\n}\n\nfunc main() {\n emp := employee{name: \"Sam\", age: 31, salary: 2000}\n\n \/\/Accessing a struct field\n n := emp.name\n fmt.Printf(\"Current name is: %s\\n\", n)\n\n \/\/Assigning a new value\n emp.name = \"John\"\n fmt.Printf(\"New name is: %s\\n\", emp.name)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nCurrent name is: Sam\nNew name is: John<\/code><\/pre>\n\n\n\n<\/span>Pointer to a struct<\/strong><\/span><\/h1>\n\n\n\nThere <\/strong>are two ways of creating a pointer to the struct<\/p>\n\n\n\n- Using the & operator<\/li><\/ul>\n\n\n\n