我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

在Go中初始化自定义int类型

In Go it is valid to create a type:

type Num int

but how can one then initialize that type? make(Num, 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.

近期文章

  • 使用标头在Golang中发送POST请求
  • 无法在现有服务器上使用Go工具pprof
  • 带导出字段的私有类型
  • 如何在Go中使用频道代替互斥体?
  • 去命名参数
  • 语法和接口作为参数起作用
  • 导入对象是预期的Golang 1.4.2中的错误
  • Golang中的iota的全名是什么?
  • Golang:如何读取ReverseProxy的响应主体?
  • 在Go中是否可以同时在通道和文件描述符上等待?
  • 取代目前的程序
  • Go中工会的最佳实践
  • 在for循环中使用指针-Golang
  • “ itoa”在strconv.Itoa中代表什么?
  • Go中的“供应”是什么意思?
  • 在个人git仓库上使用“ go get” [重复]
  • 用Go语言循环遍历数组
  • 如何在golang中将[32]字节与[]字节进行比较?
  • 如何在Golang SSH中使用加密的私钥
  • 如何在if-else中初始化错误类型

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华