0

I have the following structure:

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

I am trying to get the length of Identificator with len(event.Identificator) however I get Invalid argument for len

Before calling len I check if it's nil. I am coming from Java/C#/PHP background and it's my first time writing in GO.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user93466
  • 141
  • 2
  • 12

2 Answers2

2

There is no simple len(string) concept in Go. You either need the number of bytes in the string representation or the number of characters (so called runes). For ASCII strings both values are the same while for unicode-encoded strings they are usually different.

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

For more information you can check this answer https://stackoverflow.com/a/12668840/1201488.

UPD: event.Identificator is a pointer to a string value in the NetAuth structure rather than a string value. So you need to dereference it first via *event.Identificator.

Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • nit: ASCII strings _are_ Unicode-encoded. The proper distinction would be something like "for non-ASCII Unicode-encoded strings". – Jonathan Hall Apr 30 '18 at 07:14
  • More nit: there's no such thing as a "Unicode-encoded" string, because Unicode is not an encoding (it's more like a character set, which maps numbers to characters (or more technically correct, codepoints (which can but don't need to represent characters))). The most common Unicode _encoding_ would be UTF-8. – Thomas Apr 30 '18 at 07:22
2

You are using a pointer, so try this:

println(len(*vlr.Identificator))

For example,

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

}

Playground: https://play.golang.org/p/duf0tEddBsR

Output:

0001
4
peterSO
  • 158,998
  • 31
  • 281
  • 276
Aristofanio Garcia
  • 1,103
  • 8
  • 13
  • The official Stack Overflow advice for links: "[Provide context for links.](https://stackoverflow.com/help/how-to-answer) Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline." Plus, you get more upvotes! – peterSO Apr 29 '18 at 11:29