53

when import "golang.org/x/net/route"

it tells "build constraints exclude all Go files in go/src/golang.org/x/net/routego"

I am using VSCode editor on Windows/Linux

I searched online and didn't see a solution to the similar problem

package main

import (
    "golang.org/x/net/route"
)

{
    rib, _ := route.FetchRIB(0, route.RIBTypeRoute, 0)
    messages, err := route.ParseRIB(route.RIBTypeRoute, rib)
}
sh.seo
  • 1,482
  • 13
  • 20
jerry
  • 579
  • 1
  • 4
  • 7

12 Answers12

35

I had this problem in Goland (with source code on WSL2 and Goland on win).

I was able to solve it with

go clean -modcache

Subsequent build put everything in order again.

Andreas Gnyp
  • 1,500
  • 1
  • 19
  • 25
  • This solved my problem. And I understand that go does intelligence around what files to build, if you set the `GO_OS` or name a file like `xyz_.go`, which can lead to the unfortunate fact of having 0 files to build with even though there are source files in the pwd... However, I'm not sure what happened such that removing cached modules fixes this? What happened? – Ari Sweedler Nov 18 '22 at 16:28
  • I'm rather new to Go, so I drew from my "general experience" and speculated, after some reading here, that the cache was "mixed up". Meaning, I was installing packages using Golang AND the console. If there is some configuration going on--as you mentioned--the cache would "contradict" itself. Once again, just a thought! And I'd be very happy to amend my answer with a proper explanation, if we find something reproducible. – Andreas Gnyp Nov 19 '22 at 18:16
  • This got me mostly there. Running `go mod tidy` completed the task. – Virmundi Jun 16 '23 at 18:49
8

We met the same error under Goland, and it could be solved in this way

If you want to set GOOS = linux under Mac, just add this line in the header of file // +build linux,amd64,go1.15,!cgo which means we declare that this file is for the target system that has the following requirements: Linux, the AMD64 architecture, the 1.15 Go version, and no CGO support.


Update 08/04/2022

After go 1.17. The go command now understands //go:build lines and prefers them over // +build lines. The new syntax uses boolean expressions, just like Go, and should be less error-prone. Here are some more details

// go:build (darwin && cgo) || linux
// +build darwin,cgo linux
zangw
  • 43,869
  • 19
  • 177
  • 214
7

My issue was that I had a stray import "C" in my code and I was compiling with CGO_ENABLED=0. This problem was hard to find because IntelliJ collapses all the imports into one statement.

user2233706
  • 6,148
  • 5
  • 44
  • 86
4

try to remove folder x in go/src/golang.org/

RK26
  • 373
  • 1
  • 5
  • 20
3

In my case I just by an accident replaced original code by test file, it was containing the following comments:

//go:build unittest
// +build unittest
kvaps
  • 2,589
  • 1
  • 21
  • 20
  • upvoted! This seems like it would be a commonly overlooked issue. I had the inversion of this issue. I.E. my test file didn't have the build constraints indicating this is a test file - so my test file wasn't allowed to import our core test files. The complication in my case was my test file was importing a package which requires cgo to be enabled - so I wasted a bunch of time ensuring that cgo had all necessary prequisites enabled. – Tim Feb 26 '23 at 18:42
3

I fixed the issue by simply doing Invalidate Caches under File Tab.

fzdy1914
  • 31
  • 1
2

If you have a dependency requiring cgo (which includes x/net by default), you will get this error if your system does not have a C compiler. Can be easily fixed by installing gcc.

Lundis
  • 371
  • 1
  • 2
  • 13
  • 1
    Yeah, that's what it turned out to be for me. I was working in a new codebase (and I'm new to Go) and didn't have GCC on my box. I installed GCC and it built. The error message is misleading. – peterfelts May 12 '23 at 17:37
1

This error seems to present itself rather ambiguously.

In my case, my project uses CGO, and this error manifests when CGO_ENABLED=0 in the environment, which is solved by unsetting this env var, or setting CGO_ENABLED=1

DevOops
  • 943
  • 2
  • 10
  • 21
1

In my case it appears the go get failed and the folder referenced in GOPATH was empty. Check your library folder which is complaining here. here

MousyBusiness
  • 188
  • 2
  • 14
0

In my case, after trying the go clean -modcache solution, I also had to restart Goland before it stopped complaining about the build constraints.

Martin Meli
  • 452
  • 5
  • 18
-1

"build constraints exclude all Go files in go/src/golang.org/x/net/routego" In Intellij :

  1. Navigate to the folder in the project explorer
  2. Right click -> Mark folder as not excluded.
Sheetal Kaul
  • 3,029
  • 2
  • 13
  • 6
-2

If you get error build constraints exclude all Go files in ... with go get command, please specify the path as follows

Error:

$ go get {HOST}/platform/be-photo

Success:

$ go get {HOST}/platform/be-photo/external/client