【问题标题】:Assigning named type to literal type value将命名类型分配给文字类型值
【发布时间】:2019-10-16 14:14:58
【问题描述】:

这里是新手。我正在尝试将 uint64 值分配给具有基础类型 unit64 的命名类型的变量。编译器不高兴,看不出这两种类型完全一样。这样做的正确方法是什么?

package main

import "fmt"
import "math/rand"

type myType uint64

var x myType


func main() {
    x = rand.Uint64()
    fmt.Println(x)
}

这会引发编译器错误:

./prog.go:12:4: cannot use rand.Uint64() (type uint64) as type myType in assignment

【问题讨论】:

    标签: go


    【解决方案1】:

    您可以将其转换为myType(rand.Uint64())。有用的链接:Type Assertion in Go 和 Type Conversion in Go 以及此堆栈溢出问题中的出色答案 "How to cast to a type alias in go"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多