Coming from a php background, some elements of golang I have had to get used to. One of these was checking for the existance of a value within an array/slice.

In php, we could do the following:

$values = [
    "first",
    "second",
    "third"
];

if (!empty($values["second"])) {
    return "Hooray!";
}

In golang, it is not possible to check the existance of a string (or another value) without creating a function for this purpose. However there is a way around this.

By creating a map[string]bool, we can create a map of string/boolean, with the string as the key and true set as an arbirary boolean value. Then, we can use a normal if statement to check the existance of the string.

// main.go

func main() {
    fields := map[string]bool{
        "first": true,
        "second": true
    }

    fields["third"] = true

    if fields["third"] {
        fmt.Println("The third exists")
    }
}

// Result: The third exists