💻 Tech
Go has named and literal structs. Because Go is a strongly typed language, you can’t directly assign a value of one named struct to another; however, a literal struct can be assigned to a named struct. One practical example is passing functions. Functions are first-class citizens in Go, and they essentially behave like literal structs.
// named structs
type x struct {
flag bool
}
type y struct {
flag bool
}
func main() {
// zero-value declaration
var x1 x
var y1 y
// x1 = y1 --> error: cannot use y1 (variable of type y) as x value in assignment
x1 = x(y1) // explicit conversion; they are compatible values
x1 = struct{ flag bool }{flag: true} // literal (anonymous) struct assigned to a named struct
fmt.Println(x1, y1) // output: {true} {false}
}