Initialize custom int type in Go
In Go it is valid to create a type:
type Num int
but how can one then initialize that type? make(Num开发者_C百科, 2) does not seem to work.
Initialize the type as you would initialize the underlying type. In your example, the underlying type is an int
. For example,
package main
import (
"fmt"
)
type Num int
func main() {
var m Num = 7
n := Num(42)
fmt.Println(m, n)
}
Output: 7 42
The built-in function make takes a type T, which must be a slice, map or channel type.
精彩评论