Building UPDATE statements
If you need to update a given number of columns of a table without modifying others, you can follow the pattern given in this section.
How to do it...
- You need two pieces of information to run an UPDATE statement:
- The data to update: A common way of describing such information is to use pointers to represent updated values. Consider the following example:
type UpdateUserRequest struct { Name *string LastLogin *time.Time AvatarURL *string }
Here, a column will only be updated if the corresponding field is not null. For instance, with the following instance of
UpdateUserRequest
, only theLastLogin
andAvatarURL
fields will be updated:now:=time.Now() urlString:="https://example.org/avatar.jpg" update:=UpdateUserRequest { LastLogin: &now, AvatarURL: &urlString, }
- The record locator: This is usually the unique identifier of the row that needs to be updated. However, it is also...
- The data to update: A common way of describing such information is to use pointers to represent updated values. Consider the following example: