Search code examples
goimport

How to import package "constraints" in Go?


If you do the tour to learn go here :

https://go.dev/tour/

You may try to use constraints like Ordered.

However,

import "constraints"

returns an error :

package constraints is not in GOROOT

Solution

  • 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