In golang, structs are instantiated differently from "regular" types:
If it's a regular type: MyFloat(2)
If it's a struct: MyFloat{2}
Is there a particular reason for this?
package main
import (
"fmt"
)
type MyFloat float64
type MyFloat2 struct {
X float64
}
func main() {
f1 := MyFloat(2)
f2 := MyFloat2{3}
fmt.Println(f1)
fmt.Println(f2)
}
MyFloat(2)
is a conversion. MyFloat2{3}
is a composite literal.
Conversions can be used on structs:
var f3 struct {
X float64
}
f4 := MyFloat2(f3)