|
| 1 | +# Go Pointer |
| 2 | + |
| 3 | +[中文文档](./README.md) [English Document](./README_en.md) |
| 4 | + |
| 5 | +# 1. Use go get install it |
| 6 | + |
| 7 | +```text |
| 8 | +go get -u github.com/golang-infrastructure/go-pointer |
| 9 | +``` |
| 10 | + |
| 11 | +# 2. What problem was solved |
| 12 | + |
| 13 | +In golang, the primitive type has no wrapping type, so the primitive type can't distinguish between nil and zero, |
| 14 | +so many libraries tend to use Pointers to primitive variables to distinguish between zero and not passed. |
| 15 | + |
| 16 | +For a specific example, when no Pointers are used, there is a configuration item when executing a task: |
| 17 | + |
| 18 | +```go |
| 19 | +package main |
| 20 | + |
| 21 | +type Config struct { |
| 22 | + Foo int |
| 23 | +} |
| 24 | + |
| 25 | +``` |
| 26 | + |
| 27 | +When Foo is 0, we do not know whether we have passed zero or no value, because some libraries prefer to use pointer |
| 28 | +types in this case: |
| 29 | + |
| 30 | +```go |
| 31 | +package main |
| 32 | + |
| 33 | +type Config struct { |
| 34 | + Foo *int |
| 35 | +} |
| 36 | + |
| 37 | +``` |
| 38 | + |
| 39 | +However, sometimes this value is passed in as a literal constant, such as the paging size when querying a database. In |
| 40 | +this case, |
| 41 | +getting a pointer type can be a bit of a hassle. The above scenario is just an example of a problem that this module is |
| 42 | +designed to solve. |
| 43 | + |
| 44 | +# 3. Example Code |
| 45 | + |
| 46 | +Generics are already supported: |
| 47 | + |
| 48 | +```go |
| 49 | +package main |
| 50 | + |
| 51 | +import ( |
| 52 | + "fmt" |
| 53 | + pointer "github.com/golang-infrastructure/go-reflect-utils" |
| 54 | +) |
| 55 | + |
| 56 | +func main() { |
| 57 | + |
| 58 | + // Returns a pointer to false |
| 59 | + falsePointer := pointer.FalsePointer() |
| 60 | + fmt.Println(fmt.Sprintf("%T %v", falsePointer, *falsePointer)) // Output: *bool false |
| 61 | + |
| 62 | + // Returns a pointer to true |
| 63 | + truePointer := pointer.TruePointer() |
| 64 | + fmt.Println(fmt.Sprintf("%T %v", truePointer, *truePointer)) // Output: *bool true |
| 65 | + |
| 66 | + // Returns a pointer to the corresponding type |
| 67 | + v1 := 1 |
| 68 | + toPointer := pointer.ToPointer(v1) |
| 69 | + fmt.Println(fmt.Sprintf("%T %v", toPointer, *toPointer)) // Output: *int 1 |
| 70 | + // Returns a pointer to the corresponding type, but checks the value and returns nil if the value is zero of the corresponding type |
| 71 | + v1 = 0 |
| 72 | + orNil := pointer.ToPointerOrNil(v1) |
| 73 | + fmt.Println(orNil) // Output: nil |
| 74 | + |
| 75 | + // Reads a value from a pointer, and returns a zero value of the corresponding type if it is a nil pointer |
| 76 | + v2 := 1 |
| 77 | + v3 := &v2 |
| 78 | + fromPointer := pointer.FromPointer(v3) |
| 79 | + fmt.Println(fromPointer) // Output: 1 |
| 80 | + // Reads the value from a pointer, and returns the given default value if it is a nil pointer |
| 81 | + v2 = 0 |
| 82 | + orDefault := pointer.FromPointerOrDefault(v3, 1) |
| 83 | + fmt.Println(orDefault) // Output: 0 |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
0 commit comments