Skip to content

Instantly share code, notes, and snippets.

@diyan
Last active April 8, 2018 02:27
Show Gist options
  • Save diyan/ae3a79bdb62dca0d34b128c1b38fecfe to your computer and use it in GitHub Desktop.
Save diyan/ae3a79bdb62dca0d34b128c1b38fecfe to your computer and use it in GitHub Desktop.
sql.NullXYZ vs dbr.NullXYZ vs null.XYZ vs ntypes.XYZ vs pointer samples

sql.NullXYZ vs dbr.NullXYZ vs null.XYZ vs ntypes.XYZ vs pointer samples

Struct declarations

*string, *time

NOTE helper is optional - https://github.com/AlekSi/pointer

models.User{
  ID: 1,
  Login: "Jonh",
  Username: pointer.ToString("John Doe"),
  CreatedAt: time.Date(2999, time.January, 1, 0, 0, 0, 0, time.UTC),
  UpdatedAt: nil,
}
dbr.NullString, dbr.NullTime
models.User{
  ID: 1,
  Login: "Jonh",
  Username: dbr.NullString{NullString: sql.NullString{String: "Jonh Doe", Valid: true}},
  CreatedAt: time.Date(2999, time.January, 1, 0, 0, 0, 0, time.UTC),
  UpdatedAt: dbr.NullTime{},
}
null.String, null.Time
models.User{
  ID: 1,
  Login: "Jonh",
  Username: null.StringFrom("Jonh Doe"),
  CreatedAt: time.Date(2999, time.January, 1, 0, 0, 0, 0, time.UTC),
  UpdatedAt: null.TimeFromPtr(nil),
}
ntypes.String, ntypes.Time
models.User{
  ID: 1,
  Login: "Jonh",
  Username: ntypes.NewString("Jonh Doe"),
  CreatedAt: time.Date(2999, time.January, 1, 0, 0, 0, 0, time.UTC),
  // UpdatedAt: nil, // NOTE nullable time.Time is not supported
}

Sample ouptut using github.com/k0kubun/pp

*string, *time

NOTE helper is optional - https://github.com/AlekSi/pointer

models.User{
  ID:      1,
  Login: "John",
  Username: &"John Doe",
  CreatedAt: 2999-01-01 00:00:00 UTC,
  UpdatedAt: (*time.Time)(nil),
}
dbr.NullString, dbr.NullTime

https://github.com/gocraft/dbr

models.User{
  ID:      1,
  Login: "John",
  Username: dbr.NullString{
	NullString: sql.NullString{
	  String: "John Doe",
	  Valid:  true,
	},
  },
  CreatedAt: 2999-01-01 00:00:00 UTC,
  UpdatedAt: dbr.NullTime{
	Time:  1-01-01 00:00:00 UTC,
	Valid: false,
  },
}
null.String, null.Time

https://github.com/guregu/null

models.User{
  ID:      1,
  Login: "John",
  Username: null.String{
	NullString: sql.NullString{
	  String: "John Doe",
	  Valid:  true,
	},
  },
  CreatedAt: 2999-01-01 00:00:00 UTC,
  UpdatedAt: null.Time{
	Time:  1-01-01 00:00:00 UTC,
	Valid: false,
  },
}
ntypes.String, ntypes.Time

https://github.com/piotrkowalczuk/ntypes

models.User{
  ID:      1,
  Login: "John",
  Username: &ntypes.String{
	String: "John Doe",
	Valid:  true,
  },
  CreatedAt: 2999-01-01 00:00:00 UTC,
  // UpdatedAt: (*time.Time)(nil), // NOTE nullable time.Time is not supported
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment