2

I don't know if someone else is running into this problem. I have this main.go file:

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

when I run go build, it takes 5 secs to run it (regardless if it is the first time I run it or if it is the second time)

PS> Measure-Command {Start-Process go build -wait}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 5
Milliseconds      : 151
Ticks             : 51514117
TotalDays         : 5.96228206018519E-05
TotalHours        : 0.00143094769444444
TotalMinutes      : 0.0858568616666667
TotalSeconds      : 5.1514117
TotalMilliseconds : 5151.4117

But when I run it on a linux machine:

time go build

real    0m2.017s
user    0m0.054s
sys     0m1.915s

and when I run it for the second time:

time go build

real    0m0.120s
user    0m0.072s
sys     0m0.088s

This is not only build, but also some of go tools such as fmt. It takes 0.12 seconds on linux, but almost 3 seconds on windows. Other tools like guru, gocode, etc. suffer the same problem, making code development very slow.

I'm using golang 1.11. I'm using an SSD and everything is running locally. Sorry I wish I could be more helpful but I really have no idea where to start to debug this.

Does anyone has an idea what's going on?

Husain
  • 784
  • 1
  • 9
  • 22
  • 2
    I experience it only in first run (go run or build and run exe). Second run is instant. – Cetin Basoz Oct 17 '18 at 22:21
  • BTW, I have Goland too. If I use Goland, from within GoLand, it runs instantly whether first or not. – Cetin Basoz Oct 17 '18 at 22:34
  • what version of Go are you running? Go1.10 and later have improved build times, see [this answer](https://stackoverflow.com/a/47109826/3915). – Mark Oct 17 '18 at 23:09
  • Are you using a local drive on your Windows machine, or a network drive? – Martin Tournoij Oct 18 '18 at 01:23
  • I'm using go 1.11 on my local machine. All the files are local. Given that y'all are not experiencing this, it has to be my setup that causes this. – Husain Oct 18 '18 at 01:31
  • I have two comments. First, it's simple: just don't use MS Windows. Second, on my Macbook the first run of a go build is much slower than 5 seconds but subsequent runs are in line with Linux – Vorsprung Oct 18 '18 at 05:48
  • Completely uninstall and reinstall Go should do the trick. – Taufiq Rahman Oct 18 '18 at 06:08
  • 1
    @Husain - are you still facing this problem? I am in the same boat, I have two windows laptop, on one laptop it build takes just 2 sec, while on other it takes 10 sec. Both are high end laptops. I tried disabling antivirus, but no improvements, version 1.11.4 – Ajay Bhosale Jan 10 '19 at 02:39
  • @AjayBhosale Yes I am still facing the problem, and I could not find a solution for it.It is not a caching problem or anything. Now that you mentioned it is not an antivirus issue, I have no clue what it is. – Husain Jan 10 '19 at 14:48
  • related: [golang is slow in Windows](https://stackoverflow.com/q/32062493/10197418). for me, configuring my AntiVirus correctly did the trick. – FObersteiner Feb 03 '21 at 14:47

2 Answers2

2

It seems that the build cache is disabled on your Windows and enabled on your Linux.

Go build keeps the result of compilations and reuse it if the .go file hasn't changed. That's why your second build is so fast in Linux.

If you disable it, not only your code, but also all the dependancies must be recompiled each time. Thus even if you change your code, all the libs (here "fmt") are already in cache.

To test it, run go clean -cache before the go build on Linux, and see if the time correspond to the time on your Windows. Then if it matches, you have to find why the build cache is disabled on Windows.

You can see the cache directory by typing go env GOCACHE. If the response is off, the cache is off. Otherwise verify that the repository exists and it has the right permissions.

You can choos the cache directory by setting the value of the GOCACHE environment variable (sorry I don't know how to do this in Windows).

  • Thanks Francois. But it looks like my GOCACHE is set. so builds should be cached. – Husain Oct 20 '18 at 19:38
  • I don't think it is effective if two consecutive builds take so long, the second should be quasi instant, like in linux, except if something is wrong with the cache config. I would bet on the GOCACHE folder. Maybe it doesn't exists, or the permissions are wrong. But I can be wrong. Can you create a file in this folder with the same user that execute go build? – François Bastien Oct 20 '18 at 20:26
  • Yup. I am able to create folders using the same username. :/ I am now guessing that it is the anti-virus software installed. I don't know for sure and I can't uninstall it to test since this is the companies machine. :/ – Husain Oct 23 '18 at 13:39
0

I was having same issue. Turns out I had a really big directory with 1000s of large files in the same location where the binary was. Moving the big dir out to a different location resolved the issue. This was in a macOS.

TiN
  • 179
  • 1
  • 12