Go 获取可为空字符串的长度

Go 获取可为空字符串的长度,go,Go,我的结构如下: type NetAuth struct { Identificator *string `json:"identificator"` Password *string `json:"password"` DeviceID *string `json:"deviceId"` Type int `json:"type"` } 我试图用len(event.Identificator

我的结构如下:

type NetAuth struct {
        Identificator *string `json:"identificator"`
        Password      *string `json:"password"`
        DeviceID      *string `json:"deviceId"`
        Type          int  `json:"type"`
}
我试图用
len(event.Identificator)
获取标识符的长度,但是我得到的len参数无效


打电话给len之前,我先检查一下是不是零。我来自Java/C#/PHP背景,这是我第一次用GO写作。

GO中没有简单的
len(string)
概念。您需要字符串表示中的字节数或字符数(所谓的符文)。对于ASCII字符串,这两个值是相同的,而对于unicode编码的字符串,它们通常是不同的

import "unicode/utf8"

// Firt check that the pointer to your string is not nil
if nil != event.Identificator {
    // For number of runes:
    utf8.RuneCountInString(*event.Identificator)

    // For number of bytes:
    len(*event.Identificator)
}
有关更多信息,请查看此答案


UPD
事件。Identificator
是指向
NetAuth
结构中字符串值的指针,而不是字符串值。因此,您需要首先通过
*event.Identificator

取消对它的引用。Go中没有简单的
len(string)
概念。您需要字符串表示中的字节数或字符数(所谓的符文)。对于ASCII字符串,这两个值是相同的,而对于unicode编码的字符串,它们通常是不同的

import "unicode/utf8"

// Firt check that the pointer to your string is not nil
if nil != event.Identificator {
    // For number of runes:
    utf8.RuneCountInString(*event.Identificator)

    // For number of bytes:
    len(*event.Identificator)
}
有关更多信息,请查看此答案


UPD
事件。Identificator
是指向
NetAuth
结构中字符串值的指针,而不是字符串值。因此,您需要首先通过
*事件取消对它的引用。Identificator

如果您正在使用指针,请尝试以下操作:

println(len(*vlr.Identificator))
比如说,

package main

import (
    //"fmt"
    "encoding/json"
    "strings"
    "io"
)

type NetAuth struct {
        Identificator *string `json:"identificator"`
        Password      *string `json:"password"`
        DeviceID      *string `json:"deviceId"`
        Type          int  `json:"type"`
}

func jsondata() io.Reader {
  return strings.NewReader(`{"identificator": "0001", "password": "passkey"}`)
}

func main() {
    dec := json.NewDecoder(jsondata())
    vlr := new(NetAuth)
    dec.Decode(vlr)
    println(*vlr.Identificator)
    println(len(*vlr.Identificator))

}
游乐场:

输出:

0001
4

您正在使用指针,请尝试以下操作:

println(len(*vlr.Identificator))
比如说,

package main

import (
    //"fmt"
    "encoding/json"
    "strings"
    "io"
)

type NetAuth struct {
        Identificator *string `json:"identificator"`
        Password      *string `json:"password"`
        DeviceID      *string `json:"deviceId"`
        Type          int  `json:"type"`
}

func jsondata() io.Reader {
  return strings.NewReader(`{"identificator": "0001", "password": "passkey"}`)
}

func main() {
    dec := json.NewDecoder(jsondata())
    vlr := new(NetAuth)
    dec.Decode(vlr)
    println(*vlr.Identificator)
    println(len(*vlr.Identificator))

}
游乐场:

输出:

0001
4

您是否尝试过
len(*event.Identificator)
因为identicator是一个指针?请阅读并发布整个错误消息:
无效参数事件。len的identicator(type*string)在@xdrm括号中,如果指针为
nil
@bereal是,但您告诉我们您先检查了它,这是检查后忘记包含有问题的代码的解决方案。您是否尝试过
len(*event.identicator)
因为identicator是指针?读取并发布整个错误消息:
无效参数event.identicator(type*string)对于len
@xdrm方括号,如果指针为
nil
@bereal,它将崩溃。是的,但您告诉我们您先检查了它,这是一个解决方案,因为在您检查之后,您忘记了包含有问题的代码。有关链接的官方堆栈溢出建议:“我们鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久脱机。“此外,您还可以获得更多的升级票!官方的链接堆栈溢出建议:“鼓励链接到外部资源,但是,请在链接周围添加上下文,这样您的其他用户就会知道它是什么以及为什么会出现。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久脱机。“此外,您还可以获得更多投票权!nit:ASCII字符串是Unicode编码的。正确的区别类似于“对于非ASCII Unicode编码的字符串”。更多nit:没有“Unicode编码的字符串”这样的东西“字符串,因为Unicode不是一种编码(它更像是一个字符集,它将数字映射到字符(或者技术上更正确的是,代码点(可以但不需要表示字符)))。最常见的Unicode编码是UTF-8.nit:ASCII字符串是Unicode编码的。正确的区别可能类似于“非ASCII Unicode编码字符串”。更多细节:没有“Unicode编码”字符串,因为Unicode不是一种编码(它更像是一个字符集,它将数字映射到字符(或者更技术上正确的码点(可以但不需要表示字符)))。最常见的Unicode编码是UTF-8。