How to import package "constraints" in Go?

If you do the tour to learn go here :


You may try to use constraints like Ordered.


import "constraints"

returns an error :

package constraints is not in GOROOT


  • The "constraints" package is not part of the standard library and never was, except perhaps for early dev or beta releases before Go 1.18.

    The correct import path is:

    import "golang.org/x/exp/constraints"

    As all code in x/exp it's outside of Go compatibility guarantee.

    The reason for not including it in the standard library is mentioned here: https://github.com/golang/go/issues/50792