From aeeeb5e1fbd2f563d6b2cc0ee901c43f37e13b05 Mon Sep 17 00:00:00 2001 From: CC11001100 Date: Mon, 21 Nov 2022 11:13:27 +0800 Subject: [PATCH 1/2] add en readme --- README.md | 2 ++ README_en.md | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 README_en.md diff --git a/README.md b/README.md index 540e791..0ef7e89 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Go Pointer +[中文文档](./README.md) [英文文档](./README_en.md) + # 一、引入依赖 ```text diff --git a/README_en.md b/README_en.md new file mode 100644 index 0000000..867aa92 --- /dev/null +++ b/README_en.md @@ -0,0 +1,89 @@ +# Go Pointer + +[中文文档](./README.md) [英文文档](./README_en.md) + +# 1. Use go get install it + +```text +go get -u github.com/golang-infrastructure/go-pointer +``` + +# 2. What problem was solved + +In golang, the primitive type has no wrapping type, so the primitive type can't distinguish between nil and zero, +so many libraries tend to use Pointers to primitive variables to distinguish between zero and not passed. + +For a specific example, when no Pointers are used, there is a configuration item when executing a task: + +```go +package main + +type Config struct { + Foo int +} + +``` + +When Foo is 0, we do not know whether we have passed zero or no value, because some libraries prefer to use pointer +types in this case: + +```go +package main + +type Config struct { + Foo *int +} + +``` + +However, sometimes this value is passed in as a literal constant, such as the paging size when querying a database. In +this case, +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 +designed to solve. + +# 3. Example Code + +Generics are already supported: + +```go +package main + +import ( + "fmt" + pointer "github.com/golang-infrastructure/go-reflect-utils" +) + +func main() { + + // Returns a pointer to false + falsePointer := pointer.FalsePointer() + fmt.Println(fmt.Sprintf("%T %v", falsePointer, *falsePointer)) // Output: *bool false + + // Returns a pointer to true + truePointer := pointer.TruePointer() + fmt.Println(fmt.Sprintf("%T %v", truePointer, *truePointer)) // Output: *bool true + + // Returns a pointer to the corresponding type + v1 := 1 + toPointer := pointer.ToPointer(v1) + fmt.Println(fmt.Sprintf("%T %v", toPointer, *toPointer)) // Output: *int 1 + // Returns a pointer to the corresponding type, but checks the value and returns nil if the value is zero of the corresponding type + v1 = 0 + orNil := pointer.ToPointerOrNil(v1) + fmt.Println(orNil) // Output: nil + + // Reads a value from a pointer, and returns a zero value of the corresponding type if it is a nil pointer + v2 := 1 + v3 := &v2 + fromPointer := pointer.FromPointer(v3) + fmt.Println(fromPointer) // Output: 1 + // Reads the value from a pointer, and returns the given default value if it is a nil pointer + v2 = 0 + orDefault := pointer.FromPointerOrDefault(v3, 1) + fmt.Println(orDefault) // Output: 0 +} +``` + + + + From 74970b1266b66251186b32052c4a7967ee722e29 Mon Sep 17 00:00:00 2001 From: CC11001100 Date: Mon, 21 Nov 2022 11:14:12 +0800 Subject: [PATCH 2/2] add en readme --- README.md | 2 +- README_en.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0ef7e89..f4c297e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Go Pointer -[中文文档](./README.md) [英文文档](./README_en.md) +[中文文档](./README.md) [English Document](./README_en.md) # 一、引入依赖 diff --git a/README_en.md b/README_en.md index 867aa92..3737614 100644 --- a/README_en.md +++ b/README_en.md @@ -1,6 +1,6 @@ # Go Pointer -[中文文档](./README.md) [英文文档](./README_en.md) +[中文文档](./README.md) [English Document](./README_en.md) # 1. Use go get install it