An overview of the
You can also create a shard of a structure in Golang. In fact, shards of any data type can be created in Go. Here is a simple example of creating a structural slice
The program
package main
import "fmt"
type employee struct {
name string
age int
}
func main(a) {
employees := make([]employee, 3)
employees[0] = employee{name: "John", age: 21}
employees[1] = employee{name: "Simon", age: 25}
employees[2] = employee{name: "David", age: 18}
for _, e := range employees {
fmt.Println(e)
}
}
Copy the code
The output
{John 21}
{Simon 25}
{David 18}
Copy the code
In the above program, we created a structure named employee
type employee struct {
name string
age int
}
Copy the code
We then create a slice of the structure as shown below
employees := make([]employee, 3)
Copy the code
So that’s how we create a structural slice, right
** Note: ** Please check out our advanced Golang tutorial. This series of tutorials is carefully designed and we try to cover all the concepts with examples. This tutorial is for those who wish to gain professional knowledge and a solid understanding of Golang -Golang Advanced Tutorial
If you are interested in learning how to implement all design patterns in Golang. If so, then this article is for you — All Design Patterns Golang
The postSlice of Struct in Go (Golang)appeared first onWelcome To Golang By Example.