课程: Hands-On Introduction: Go

今天就学习课程吧!

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

Standard library support

Standard library support

- Go standard library ships with some builtin type constraints that you can take advantage of right away. One of which is the comparable interface. Let's pull up ekg.go.dev and let's go to slash STD to get to the standard library. And we'll click on builtin and we'll expand types and look for type comparable. Here we are. So comparable is implemented by all comparable types, including bullions, numbers, strings, pointers, channels arrays of comparable types, instructs whose fields are also comparable types. Let's switch back to our code. Let's now write a trivial bullion returning function named equal that accepts A and B of type T where T is constrained by all comparable types. We'll jump to line 20 and write our equal function. Type parameter list brackets T and comparable. We'll accept A and B of type T and return on bullion. And then now we'll simply do is A equal to B using the equality operator. We'll…

内容