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

44. reflect #44

Open
guonaihong opened this issue Feb 27, 2024 · 0 comments
Open

44. reflect #44

guonaihong opened this issue Feb 27, 2024 · 0 comments

Comments

@guonaihong
Copy link
Owner

记录reflect相关的碎片化知识点,以及自问自答

1. IsZero接口

1.1 IsZero接口

如果struct是编译时的值是空的,IsZero是知道的。
下面的内容摘自IsZero的实现,编译器版本是go1.21.0

case Struct:
		// If the type is comparable, then compare directly with zero.
		if v.typ().Equal != nil && v.typ().Size() <= maxZero {
			if v.flag&flagIndir == 0 {
				return v.ptr == nil
			}
			// See noescape justification above.
			return v.typ().Equal(noescape(v.ptr), unsafe.Pointer(&zeroVal[0]))
		}

		n := v.NumField()
		for i := 0; i < n; i++ {
			if !v.Field(i).IsZero() {
				return false
			}
		}
		return true
1.2 TODO 要了解的点

看下编译的时候如何填充.Equal和Size字段的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant