开发者

Initialize custom int type in Go

开发者 https://www.devze.com 2023-03-06 17:22 出处:网络
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

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

精彩评论

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