I highly suggest you to take a look at Types in Go.
Also, to set a variable to a custom type, you have to declare it manually.
In this case, it should be var x hello = 0
package main
import "fmt"
type hello int32
func main() {
var x hello = 0 // converting int to hello
fmt.Printf("Type : %T, Value: %v", x, x) // prints "Type: main.hello, Value: 0"
}
Now to convert a numeric type to hello
type, use the hello(x)
function (it's already built-in since you have the custom hello
type)
Let's suppose you have the following code:
package main
import "fmt"
type hello int32
func main() {
var x int = 0
fmt.Printf("Type : %T, Value: %v", x, x) // prints "Type: int, Value: 0"
}
Here, we will convert x
to hello
type. To do it, just use hello(x)
.
package main
import "fmt"
type hello int32
func main() {
var x int = 0
y := hello(x) // converting 'x' to 'hello' type
fmt.Printf("Type : %T, Value: %v", y, y) // prints "Type: main.hello, Value: 0"
}