开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜