课程: Generics in Go

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Experimental package: Constraints

Experimental package: Constraints - Go教程

课程: Generics in Go

Experimental package: Constraints

- [Instructor] Go 1.18 introduced the new package constraint. Let's look into it. The experimental Go package constraint provides useful constraints for type parameters that consist of pre-declared types. Remember, a pre-declared type is a type of the Go standard library, such as integer and float. The constraint Complex is for all pre-declared complex numbers, namely complex64 and complex128, and will be updated if new complex types are added to the standard library. Note the tilde before both types. The tilde allows the subtypes of the type afterwards. One practical example of this constraint is for a program that handles quantum mechanics. To present a point, a complex type is unavoidable. However, it may be okay to use any pre-declared complex number. The constraint Float can handle all floating-point numbers, concretely, the floating-point numbers, float32 and float64, and type subtypes. As before, future…

内容