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
How do you send an HTTP GET request with an HTTP client in Go?
Print inverted full pyramid using star
GO Program to Calculate Sum of Natural Numbers Using for.....Loop
How to Convert string to integer type in Go?
Go program to find Name Server (NS) record of a domain
Illustration of Checkpoint Synchronization in Golang