We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
目前 golang 无法支持对泛型参数空检查,例如下面代码就会报错:
func IsNil[T any](t T) bool { // invalid operation: t == nil (mismatched types T and untyped nil) return t == nil }
解决方式只能把参数 T 改为指针:
// Works now func IsNil[T any](t *T) bool { return t == nil }
但是如果我们需要写一个函数可以把任意参数转化为指针,比如 aws sdk go v2中几乎所有参数都是指针类型,所以提供了一系列的函数,例如取 string 指针:
// String returns a pointer value for the string value passed in. func String(v string) *string { return ptr.String(v) }
如果写一个泛型的取指针的函数就会遇到上面 IsNil 的问题。
如果不使用 *T 参数的话,这里有一个取巧的方式,使用 refelct 包:
func TakePoint[T any](t T) *T { if reflect.TypeOf(t) == nil { return nil } return &t }
reflect.TypeOf(i) 如果参数 i 是 nil 那么也会返回 nil,所以可以利用这个特性解决这个问题。
reflect.TypeOf(i)
附上一个简单的测试用例
func TestTakePoint(t *testing.T) { if res := TakePoint[string]("string"); *res != "string" { t.Fatal("string") } if res := TakePoint[int](100); *res != 100 { t.Fatal("int") } if res := TakePoint[any](nil); res != nil { t.Fatal("fail nil") } }
可以关注这个 issue 22729 和 issue 61489 进行跟进。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
目前 golang 无法支持对泛型参数空检查,例如下面代码就会报错:
解决方式只能把参数 T 改为指针:
但是如果我们需要写一个函数可以把任意参数转化为指针,比如 aws sdk go v2中几乎所有参数都是指针类型,所以提供了一系列的函数,例如取 string 指针:
如果写一个泛型的取指针的函数就会遇到上面 IsNil 的问题。
如果不使用 *T 参数的话,这里有一个取巧的方式,使用 refelct 包:
reflect.TypeOf(i)
如果参数 i 是 nil 那么也会返回 nil,所以可以利用这个特性解决这个问题。附上一个简单的测试用例
可以关注这个 issue 22729 和 issue 61489 进行跟进。
The text was updated successfully, but these errors were encountered: