Created
December 16, 2022 17:06
-
-
Save insomnius/9aa38a3017c597ea5c293b5b27a7444c to your computer and use it in GitHub Desktop.
Golang Generic for Value to Pointer Function
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 main | |
import "fmt" | |
type SomeStruct struct{} | |
type Value interface { | |
int | float64 | SomeStruct | string | |
} | |
func main() { | |
exampleString := "Hello, World!" | |
exampleInt := 42 | |
exampleFloat := 3.14 | |
exampleStructToPointer := SomeStruct{} | |
fmt.Println("StringToPointer:", StringToPointer(exampleString)) | |
fmt.Println("IntToPointer:", IntToPointer(exampleInt)) | |
fmt.Println("FloatToPointer:", FloatToPointer(exampleFloat)) | |
fmt.Println("SomeStructToPointer:", SomeStructToPointer(exampleStructToPointer)) | |
fmt.Println("=====================================") | |
fmt.Println("PointerToString:", PointerToString(StringToPointer(exampleString))) | |
fmt.Println("PointerToInt:", PointerToInt(IntToPointer(exampleInt))) | |
fmt.Println("PointerToFloat:", PointerToFloat(FloatToPointer(exampleFloat))) | |
fmt.Println("PointerToSomeStruct:", PointerToSomeStruct(SomeStructToPointer(exampleStructToPointer))) | |
fmt.Println("=====================================") | |
fmt.Println("StringToPointer with generic:", ValueToPointer(exampleString)) | |
fmt.Println("IntToPointer with generic:", ValueToPointer(exampleInt)) | |
fmt.Println("FloatToPointer with generic:", ValueToPointer(exampleFloat)) | |
fmt.Println("SomeStructToPointer with generic:", ValueToPointer(exampleStructToPointer)) | |
fmt.Println("=====================================") | |
fmt.Println("PointerToString with generic:", PointerToValue(ValueToPointer(exampleString))) | |
fmt.Println("PointerToInt with generic:", PointerToValue(ValueToPointer(exampleInt))) | |
fmt.Println("PointerToFloat with generic:", PointerToValue(ValueToPointer(exampleFloat))) | |
fmt.Println("PointerToSomeStruct with generic:", PointerToValue(ValueToPointer(exampleStructToPointer))) | |
} | |
func StringToPointer(s string) *string { | |
return &s | |
} | |
func IntToPointer(i int) *int { | |
return &i | |
} | |
func FloatToPointer(f float64) *float64 { | |
return &f | |
} | |
func SomeStructToPointer(s SomeStruct) *SomeStruct { | |
return &s | |
} | |
func PointerToString(s *string) string { | |
if s == nil { | |
return "" | |
} | |
return *s | |
} | |
func PointerToInt(i *int) int { | |
if i == nil { | |
return 0 | |
} | |
return *i | |
} | |
func PointerToFloat(f *float64) float64 { | |
if f == nil { | |
return 0 | |
} | |
return *f | |
} | |
func PointerToSomeStruct(s *SomeStruct) SomeStruct { | |
if s == nil { | |
return SomeStruct{} | |
} | |
return *s | |
} | |
func ValueToPointer[V Value](v V) *V { | |
return &v | |
} | |
func PointerToValue[V Value](v *V) V { | |
if v == nil { | |
return *new(V) | |
} | |
return *v | |
} | |
// Visit my article https://insomnius.github.io/golang/2022/12/16/golang-generic-for-value-to-pointer-function/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment