I’m defining a type. I notice that Go has a type called uint8 and a function called uint8 which creates a uint8 value.
But when I try to do this for myself:
12: type myType uint32
14: func myType(buffer []byte) (result myType) { ... }
I get the error
./thing.go:14: myType redeclared in this block
previous declaration at ./thing.go:12
If I change it to func newMyType that works but it feels like I’m a second class citizen. Can I write type constructor functions with the same ident as type type?
uint8()isn’t a function nor a constructor, but a type conversion.For a primitive type (or other obvious conversions but I don’t know the exact law), you don’t need to create a constructor.
You can simply do this :
If you have operations to do when creating your value, you should use a “make” function :
Naming a function
newMyTypeshould only be used when returning a pointer.