-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added helper to improve readability of pointer literals (#535)
* Added helper to improve readability of pointer literals * Added comments an examples of how to use pointer
- Loading branch information
1 parent
bd61260
commit 1328865
Showing
5 changed files
with
78 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package linodego | ||
|
||
/* | ||
Pointer takes a value of any type T and returns a pointer to that value. | ||
Go does not allow directly creating pointers to literals, so Pointer enables | ||
abstraction away the pointer logic. | ||
Example: | ||
booted := true | ||
createOpts := linodego.InstanceCreateOptions{ | ||
Booted: &booted, | ||
} | ||
can be replaced with | ||
createOpts := linodego.InstanceCreateOptions{ | ||
Booted: linodego.Pointer(true), | ||
} | ||
*/ | ||
|
||
func Pointer[T any](value T) *T { | ||
return &value | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package linodego | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
// TestPointer tests the Pointer helper function with various types | ||
func TestPointer(t *testing.T) { | ||
// Test with an integer | ||
intValue := 11 | ||
intPtr := Pointer(intValue) | ||
if *intPtr != intValue { | ||
t.Errorf("Expected %d, got %d", intValue, *intPtr) | ||
} | ||
|
||
// Test with a float | ||
floatValue := 1.23 | ||
floatPtr := Pointer(floatValue) | ||
if *floatPtr != floatValue { | ||
t.Errorf("Expected %f, got %f", floatValue, *floatPtr) | ||
} | ||
|
||
// Test with a string | ||
stringValue := "hello world" | ||
stringPtr := Pointer(stringValue) | ||
if *stringPtr != stringValue { | ||
t.Errorf("Expected %s, got %s", stringValue, *stringPtr) | ||
} | ||
|
||
// Test with a boolean | ||
boolValue := true | ||
boolPtr := Pointer(boolValue) | ||
if *boolPtr != boolValue { | ||
t.Errorf("Expected %t, got %t", boolValue, *boolPtr) | ||
} | ||
|
||
// Test with a struct | ||
type myStruct struct { | ||
Field1 int | ||
Field2 string | ||
} | ||
structValue := myStruct{Field1: 1, Field2: "test"} | ||
structPtr := Pointer(structValue) | ||
if structPtr.Field1 != structValue.Field1 || structPtr.Field2 != structValue.Field2 { | ||
t.Errorf("Expected %+v, got %+v", structValue, *structPtr) | ||
} | ||
} |
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
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
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