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 to build a map of struct and append values to it?
Interface embedding another interface in Go Programming Language
10 Countries Paying Highest Salaries to Cybersecurity Professionals
GO Program to find area and circumference of circle
How do you create an HTTP server in Go?
What is the default HTTP server in Go?
Different ways for Integer to String Conversions
GO Program to Find the Largest Number Among Three Numbers
Golang program for implementation of Comb Sort
Make Your Retirement Luxurious with These 5 Game-Changing Altcoins