0

I am trying to run this code:

type NullInt64 struct {
    sql.NullInt64
}

func ToNullInt64(s string) NullInt64 {
    i, err := strconv.Atoi(s)
    return NullInt64{Int64: int64(i), Valid: err == nil}
}

but I get this error:

..\sql\sql.go:27: unknown NullInt64 field 'Int64' in struct literal
..\sql\sql.go:27: unknown NullInt64 field 'Valid' in struct literal
Vandal
  • 903
  • 1
  • 14
  • 27
Alex
  • 5,671
  • 9
  • 41
  • 81

1 Answers1

4

To initialize the embedded sql.NullInt64, you have to write:

NullInt64{sql.NullInt64{Int64: int64(i), Valid: err == nil}}

or, if your NullInt64 struct contains other fields which you don't want to initialize explicitly, you can access the embedded field by using its type:

NullInt64{NullInt64: sql.NullInt64{Int64: int64(i), Valid: err == nil}}
rob74
  • 4,939
  • 29
  • 31