In the previous blog, we learned about Golang Map Syntax and Creation, Deletion, and Map Manipulation. This time will learn about Golang Struct, syntax, and creation with examples.
What we will learn in Golang Struct Syntax and Creation blog:
- What is Golang Struct? Why Struct is needed in Golang?
- Creation of Golang Struct.
- Naming Convention of Golang Struct.
- Embedding in Golang Struct
- Tags in Golang Struct
Golang Struct Syntax
Instantiating struct in Golang using the keyword argument.
package main
import (
"fmt"
)
type Student struct {
rollno int
name string
marks map[string]int
}
func main() {
ds := Student{
rollno: 17,
name: "Divyanshu Shekhar",
marks: map[string]int{
"Physics": 99,
"Chemistry": 90,
"Mathematics": 90,
"Computer Science":100,
},
}
fmt.Println(ds)
}
Instantiating struct using the positional Arguments.
Use of Positional Arguments is not suggested as it may become hard to maintain the code.
ds := Student{
17,
"Divyanshu Shekhar",
map[string]int{
"Physics": 99,
"Chemistry": 90,
"Mathematics": 90,
"Computer Science":100,
},
}
Output:
{17 Divyanshu Shekhar map[Chemistry:90 Computer Science:100 Mathematics:90 Physics:99]}
Golang Structs Dereferencing
we can access struct values using the dot operator.
<struct_name>.<field_name>
fmt.Println(ds.name)
Output:
Divyanshu Shekhar
Golang Structs Naming Convention
PascalCase if you want to export the Struct from the package.
camelCase if you want to use it in the same package.
Golang Anonymous Struct
ds := struct{ name string }{name: "Divyanshu Shekhar"}
fmt.Println(ds)
Output:
{Divyanshu Shekhar}
Golang Structs Value Type
Go Structs are of the value type. it means if you assign a struct to another struct, the whole struct will be copied and any changes done in the copied struct will not affect the original struct.
Example:
package main
import (
"fmt"
)
type Student struct {
rollno int
name string
marks []int
}
func main() {
ds := Student{
rollno: 17,
name: "Divyanshu Shekhar",
marks: []int{99, 100, 98, 97, 95},
}
as := ds
as.rollno = 15
as.name = "Arpit Sahu"
as.marks = []int{98, 99, 97, 96, 94}
fmt.Println(ds)
fmt.Println(as)
}
Output:
{17 Divyanshu Shekhar [99 100 98 97 95]}
{15 Arpit Sahu [98 99 97 96 94]}
Golang Struct Embedding
Golang doesn’t have inheritance and it provides us with Struct embedding.
Inheritance sets up a ‘is a‘ relationship while embedding sets up a ‘has a‘ relationship.
package main
import (
"fmt"
)
type Language struct {
creator string
year int
}
type Python struct {
// Embedding Language inside Python Struct
Language
name string
}
func main() {
py := Python{}
py.creator = "Guido van Rossum"
py.year = 1990
py.name = "Python"
fmt.Println(py)
// fmt.Println(as)
}
OR
py := Python{
Language: Language{creator: "Guido van Rossum", year: 1990},
name: "Python",
}
Output:
{{Guido van Rossum 1990} Python}
Golang Struct Tags
Struct Tags are accessed by reflection package in Golang.
package main
import (
"fmt"
"reflect"
)
type Language struct {
name string `required max:"100"`
year int
}
func main() {
t := reflect.TypeOf(Language{})
field, _ := t.FieldByName("name")
fmt.Println(field.Tag)
}
Output:
required max:”100″
Learn more about Golang structs syntax and creation from the official documentation.