Creating a Struct Instance Using a Struct Literal
Creates an instance of rectangle struct by using a struct literal and assigning values to the fields of the struct.
Example
package main
import "fmt"
type rectangle struct {
length int
breadth int
color string
}
func main() {
var rect1 = rectangle{10, 20, "Green"}
fmt.Println(rect1)
var rect2 = rectangle{length: 10, color: "Green"} // breadth value skipped
fmt.Println(rect2)
rect3 := rectangle{10, 20, "Green"}
fmt.Println(rect3)
rect4 := rectangle{length: 10, breadth: 20, color: "Green"}
fmt.Println(rect4)
rect5 := rectangle{breadth: 20, color: "Green"} // length value skipped
fmt.Println(rect5)
}
Most Helpful This Week
Cannot use <variable> as <type> value in return statement error in Golang
Golang program to demonstrates how to encode map data into a JSON string.
Copy Struct Type Using Value and Pointer Reference
Go program to find CNAME record of a domain
How to create an empty Slice in Golang?
Golang program for implementation LIFO Stack and FIFO Queue