How check if a property was set in a struct

Like dyoo said, you can use nil if your struct properties are pointers. If you want to keep them as strings you can compare with "". Here is a sample:

package main

import "fmt"

type MyStruct struct {
    Property string
}

func main() {
    s1 := MyStruct{
        Property: "hey",
    }

    s2 := MyStruct{}

    if s1.Property != "" {
        fmt.Println("s1.Property has been set")
    }

    if s2.Property == "" {
        fmt.Println("s2.Property has not been set")
    }
}

http://play.golang.org/p/YStKFuekeZ

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)