The sql.NullString type is not actually a string type but a struct type. It's defined as:
type NullString struct { String string Valid bool // Valid is true if String is not NULL }
Therefore you need to initialize it as such:
db.Create(&Day{ Nameday: "Monday", Dateday: "23-10-2019", Something: sql.NullString{String: "a string goes here", Valid: true}, Holyday: false, })
As an alternative, if you want to keep using the simpler syntax when initializing a nullable string, you could declare your own nullable string type, have it implement the sql.Scanner and driver.Valuer interfaces, and leverage the null byte to signal a NULL value.
type MyString string const MyStringNull MyString = "\x00" // implements driver.Valuer, will be invoked automatically when written to the db func (s MyString) Value() (driver.Value, error) { if s == MyStringNull { return nil, nil } return []byte(s), nil } // implements sql.Scanner, will be invoked automatically when read from the db func (s *MyString) Scan(src interface{}) error { switch v := src.(type) { case string: *s = MyString(v) case []byte: *s = MyString(v) case nil: *s = MyStringNull } return nil }
With this, if you declare the field Something to be of type MyString you can initialize it as you originally intended.
db.Create(&Day{ Nameday: "Monday", Dateday: "23-10-2019", // here the string expression is an *untyped* string constant // that will be implicitly converted to MyString because // both `string` and `MyString` have the same *underlying* type. Something: "a string goes here", Holyday: false, })
Just keep in mind that this works only with untyped constants, once you have a constant or variable of type string, to be able to assign that to a MyString you'll need to use an explicit conversion.
var s string var ms MyString s = "a string goes here" ms = s // won't compile because s is not an untyped constant ms = MyString(s) // you have to explicitly convert
sql.NullStringis astructtype and is not convertible to astringtype. You need to initialize a value ofsql.NullStringand set that to the field. One way to do that would beSomething: sql.NullString{String: "a string goes here", Valid: true}. golang.org/pkg/database/sql/#NullString*stringtype forNULL-able columns