获取可空字符串的长度

时间:2018-04-29 10:44:41

标签: go

我有以下结构:

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

我正在尝试使用len(event.Identificator)来获取 Identificator 的长度,但是我得到 len的无效参数

在致电len之前,我会检查它是否为零。我来自Java / C#/ PHP背景,这是我第一次在GO写作。

2 个答案:

答案 0 :(得分:2)

Go中没有简单的from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get("http://www.python.org") assert "Python" in driver.title elem = driver.find_element_by_name("q") elem.clear() elem.send_keys("pycon") elem.send_keys(Keys.RETURN) assert "No results found." not in driver.page_source driver.close() 概念。您需要字符串表示中的字节数或字符数(所谓的符文)。对于ASCII字符串,两个值都相同,而对于unicode编码的字符串,它们通常是不同的。

void VrsRequesterWrapper::parseHardwareData(QString pData)
 {
QXmlStreamReader reader(pData);

if(reader.readNextStartElement()) {
    if(reader.name() == "unit" || reader.name() == "unit2") {

        while(reader.readNextStartElement()) {
             qDebug() << " first Item #1" << reader.name();
            if(reader.name() == "unit_type") {
                qDebug() << "Unit Element " << reader.name();
                 reader.skipCurrentElement();
            }
            if(reader.name() == "manufacturer") {
                    qDebug() << "Unit Element #1 " << reader.name();
                     reader.skipCurrentElement();
            }
            if(reader.name() == "article_number") {
                    qDebug() << "Unit Element #2 " << reader.name();
                    reader.skipCurrentElement();
            }
            if(reader.name() == "production_date") {
                    qDebug() << "Unit Element #3 " << reader.name();
                     reader.skipCurrentElement();
            }
            if(reader.name() == "delivery_revision") {
                    qDebug() << "Unit Element #4 " << reader.name();
                     reader.skipCurrentElement();
            }
            if(reader.name() == "present_revision") {
                    qDebug() << "Unit Element #5 " << reader.name();
                     reader.skipCurrentElement();
            }
            if(reader.name() == "rev_date") {
                    qDebug() << "Unit Element #6 " << reader.name();
                     reader.skipCurrentElement();
            }
            if(reader.name() == "made_the_last_rev") {
                    qDebug() << "Unit Element #7 " << reader.name();
                     reader.skipCurrentElement();
            }
            if(reader.name() == "serial_number") {
                    qDebug() << "Unit Element #8 " << reader.name();
                     reader.skipCurrentElement();
            }
            if(reader.name() == "eth_addresses") {
                while(reader.readNextStartElement()) {
                    if(reader.name() == "eth_address") {
                        qDebug() << "Ethernet Element #1 " << reader.name();
                        reader.skipCurrentElement();
                    }

                }
            }
            if(reader.name() == "boards") {
                qDebug() << "Boards " << reader.name();
                while(reader.readNextStartElement()) {
                    qDebug() << "Boards #1" << reader.name();
                    if(reader.name() == "board") {
                        while(reader.readNextStartElement()) {
                            if(reader.name() == "board_type") {
                                qDebug() << "Boards #2" << reader.name();
                                reader.skipCurrentElement();
                            }
                            if(reader.name() == "article_number") {
                                qDebug() << "Boards #3" << reader.name();
                                reader.skipCurrentElement();
                            }
                            if(reader.name() == "production_date") {
                                qDebug() << "Boards #4" << reader.name();
                                reader.skipCurrentElement();
                            }
                            if(reader.name() == "delivery_revision") {
                                qDebug() << "Boards #5" << reader.name();
                                reader.skipCurrentElement();
                            }
                            if(reader.name() == "present_revision") {
                                qDebug() << "Boards #6" << reader.name();
                                reader.skipCurrentElement();
                            }
                            if(reader.name() == "rev_date") {
                                qDebug() << "Boards #7" << reader.name();
                                reader.skipCurrentElement();
                            }
                            if(reader.name() == "made_the_last_rev") {
                                qDebug() << "Boards #8" << reader.name();
                                reader.skipCurrentElement();
                            }
                            if(reader.name() == "serial_number") {
                                qDebug() << "Boards #9" << reader.name();
                                reader.skipCurrentElement();
                            }
                        }
                    }
                }
            }
        }

    }
}
}

有关详细信息,请查看此答案https://stackoverflow.com/a/12668840/1201488

UPD len(string)是指向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) } 结构中字符串值的指针,而不是字符串值。因此,您需要先通过event.Identificator取消引用它。

答案 1 :(得分:2)

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

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))

}

游乐场:tf.layers.batch_normalization()

输出:

0001
4