Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 无法将地址(类型字符串)用作分配中的AccountAddress类型_Go - Fatal编程技术网

Go 无法将地址(类型字符串)用作分配中的AccountAddress类型

Go 无法将地址(类型字符串)用作分配中的AccountAddress类型,go,Go,分配类型为type AccountAddress[16]uint8的字符串时,我遇到了一个问题 type AccountAddress [16]uint8 address := c.Param("adress") var account AccountAddress account = address 尝试: var account diemtypes.AccountAddress account = []uint8(address) 有人能帮我一下吗。字符串可以隐

分配类型为
type AccountAddress[16]uint8
的字符串时,我遇到了一个问题

type AccountAddress [16]uint8

address := c.Param("adress")
var account AccountAddress
account = address
尝试:

 var account diemtypes.AccountAddress
 account = []uint8(address)

有人能帮我一下吗。

字符串可以隐式转换为[]uint8,但这是一个片,[16]uint8是一个数组。这些是不同的和不兼容的类型。但是,您可以使用
copy()
函数将切片的内容复制到数组中


请看这里的一个例子:

这里您混淆了许多基本的东西。让我一个一个地把它们弄清楚

  • 在golang中,自定义类型被视为单独的类型,即使它们都是使用相同的内置类型定义的
  • 比如说

        type StringType1 string 
        type StringType2 string
        
        var name1 StringType1="Umar"
        var name2 StringType2="Hayat"
        
        fmt.Println("StringType1 : ", name1)
        fmt.Println("StringType2 : ", name2)
    
    上述代码的输出是

    这里我们有两个自定义类型
    StringType1
    StringType2,它们都是由
    string`定义的。但是我们不能将这两种类型的变量直接分配给彼此,除非我们将它们转换为所需的类型,例如

        name1=name2
    
    
    输出

    cannot use name2 (type StringType2) as type StringType1 in assignment
    
    您的代码也是如此。您正在将
    字符串
    转换为自定义类型,而不应用转换

  • 其次,您试图将
    字符串
    转换为固定长度为15的字节数组,并键入
    uint8
    。这可以按照@Cerise Limon所述完成

  • 但是请记住,您正在将
    字符串
    复制到
    unit8
    类型中,因此您不应该在
    帐户
    中使用字符,而是该字符串的ASCI值。我希望它能消除您所有的误解。

    根据您的类型定义,分配给
    AccountAddress
    的值必须是16个8位无符号整数的数组,而不是字符串。
    地址的值是多少?如果这个字符串编码为16字节,您可能可以转换(不确定这在您的上下文中是否有意义)。@MrFuppes
    address
    是一个包含随机字符串
    copy(account[:],c.Param(“address”)
    的字符串。您能给我举个例子吗?我用一个例子更新了我的帖子
    StringType1 :  Umar
    StringType2 :  Hayat
    
        name1=name2
    
    
    cannot use name2 (type StringType2) as type StringType1 in assignment
    
    copy(account[:], c.Param("adress"))