Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I represent an Optional String in Go?

I wish to model a value which can have two possible forms: absent, or a string.

The natural way to do this is with Maybe String, or Optional<String>, or string option, etc. However, Go does not have variant types like this.

I then thought, following Java, C, etc., that the alternative would be nullability, or nil in Go. However, nil is not a member of the string type in Go.

Searching, I then thought to use the type *string. This could work, but seems very awkward (e.g. I cannot take the address of the string literal in the same way that I can take the address of a struct literal).

What is the idiomatic way to model such a value in Go?

like image 434
jameshfisher Avatar asked Jun 09 '15 12:06

jameshfisher


People also ask

How are string represented in Go?

Go supports two styles of string literals, the double-quote style (or interpreted literals) and the back-quote style (or raw string literals). The zero values of string types are blank strings, which can be represented with "" or `` in literal. Strings can be concatenated with + and += operators.

Does Go have optionals?

Optional is a library that provides option types for the primitive Go types. It can also be used as a tool to generate option type wrappers around your own types.

What is null Golang?

nil is a predefined identifier in Go that represents zero values of many types. nil is usually mistaken as null (or NULL) in other languages, but they are different. Note: nil can be used without declaring it.


1 Answers

A logical solution would be to use *string as mentioned by Ainar-G. This other answer details the possibilities of obtaining a pointer to a value (int64 but the same works for string too). A wrapper is another solution.

Using just string

An optional string means a string plus 1 specific value (or state) saying "not a string" (but a null).

This 1 specific value can be stored (signaled) in another variable (e.g bool) and you can pack the string and the bool into a struct and we arrived to the wrapper, but this doesn't fit into the case of "using just a string" (but is still a viable solution).

If we want to stick to just a string, we can take out 1 specific value from the possible values of a string type (which has "infinity" possible values as the length is not limited (or maybe it is as it must be an int but that's all right)), and we can name this specific value the null value, the value which means "not a string".

The most convenient value for indicating null is the zero value of string, which is the empty string: "". Designating this the null element has the convenience that whenever you create a string variable without explicitly specifying the initial value, it will be initialized with "". Also when querying an element from a map whose value is string will also yield "" if the key is not in the map.

This solution suits many real-life use-cases. If the optional string is supposed to be a person's name for example, an empty string does not really mean a valid person name, so you shouldn't allow that in the first place.

There might be cases of course when the empty string does represent a valid value of a variable of string type. For these use-cases we can choose another value.

In Go, a string is in effect a read-only slice of bytes. See blog post Strings, bytes, runes and characters in Go which explains this in details.

So a string is a byte slice, which is the UTF-8 encoded bytes in case of a valid text. Assuming you want to store a valid text in your optional string (if you wouldn't, then you can just use a []byte instead which can have a nil value), you can choose a string value which represents an invalid UTF-8 byte sequence and thus you won't even have to make a compromise to exclude a valid text from the possible values. The shortest invalid UTF-8 byte sequence is 1 byte only, for example 0xff (there are more). Note: you can use the utf8.ValidString() function to tell if a string value is a valid text (valid UTF-8 encoded byte sequence).

You can make this exceptional value a const:

const Null = "\xff"

Being this short also means it will be very fast to check if a string equals to this.
And by this convention you already have an optional string which also allows the empty string.

Try it on the Go Playground.

const Null = "\xff"

func main() {
    fmt.Println(utf8.ValidString(Null)) // false

    s := Null
    fmt.Println([]byte(s)) // [255]
    fmt.Println(s == Null) // true
    s = "notnull"
    fmt.Println(s == Null) // false
}
like image 140
icza Avatar answered Oct 19 '22 12:10

icza