Created
April 15, 2020 06:20
-
-
Save shogo82148/14d7ea62e03b1f84a7adf995eef254bf to your computer and use it in GitHub Desktop.
snippet for handling pointers to built-in types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package null | |
import "time" | |
func stringPtr(v string) *string { | |
return &v | |
} | |
func stringValue(v *string) string { | |
if v == nil { | |
return "" | |
} | |
return *v | |
} | |
func intPtr(v int) *int { | |
return &v | |
} | |
func intValue(v *int) int { | |
if v == nil { | |
return 0 | |
} | |
return *v | |
} | |
func boolPtr(v bool) *bool { | |
return &v | |
} | |
func boolValue(v *bool) bool { | |
if v == nil { | |
return false | |
} | |
return *v | |
} | |
func timePtr(v time.Time) *time.Time { | |
return &v | |
} | |
func timeValue(v *time.Time) time.Time { | |
if v == nil { | |
return time.Time{} | |
} | |
return *v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment