Skip to content
New issue

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 generic nil check 泛型空指针检查 #254

Open
islishude opened this issue Aug 2, 2023 · 0 comments
Open

golang generic nil check 泛型空指针检查 #254

islishude opened this issue Aug 2, 2023 · 0 comments
Labels

Comments

@islishude
Copy link
Owner

islishude commented Aug 2, 2023

目前 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,所以可以利用这个特性解决这个问题。

附上一个简单的测试用例

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 22729issue 61489 进行跟进。

@islishude islishude added the Go label Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant