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
Different ways for Integer to String Conversions
GO Program to find area and circumference of circle
Cannot use <variable> as <type> value in return statement error in Golang
Golang program to print all Permutations of a given string
How to access slice items in Golang?
Defining a type that satisfies an interface in Go Programming Language