Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Setup "Go" environment on Windows

I'm trying to setup Go on my Windows machine,

I followed the instruction on this page https://golang.org/doc/code.html#Workspaces to launch hello.go and face some difficulties.

So, I downloaded and installed MSI file

Here is my Go version: go version go1.12.4 windows/amd64

my go env:

set GOARCH=amd64
set GOBIN=F:\GoWorckspace\bin
set GOCACHE=C:\Users\Avetis\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=F:\GoWorckspace
set GOPROXY=
set GORACE=
set GOROOT=E:\Go
set GOTMPDIR=
set GOTOOLDIR=E:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Avetis\AppData\Local\Temp\go-build714242958=/tmp/go-build -gno-record-gcc-switches

GOPATH dir structure is: -bin/ -pkg/ -src/ -hello/ hello.go

hello.go content:

package main

  import (
      "fmt"
  )

  func main() {
      fmt.Println("Hello World!")
  }

When I try go install I get this arror:

 F:\GoWorckspace\src\hello> go install hello.go
open C:\Users\Avetis\AppData\Local\Temp\go-build786217674\b001\exe\a.out.exe: The system cannot find the file specified.

I have tried different options, but all of them lead to the same error:

PS F:\GoWorckspace\src> go install .\hello\hello.go
open C:\Users\Avetis\AppData\Local\Temp\go-build220985190\b001\exe\a.out.exe: The system cannot find the file specified.
PS F:\GoWorckspace\src> go install .\hello
open C:\Users\Avetis\AppData\Local\Temp\go-build247169354\b001\exe\a.out.exe: The system cannot find the file specified.
PS F:\GoWorckspace\src> go install hello
open C:\Users\Avetis\AppData\Local\Temp\go-build365645162\b001\exe\a.out.exe: The system cannot find the file specified.
PS F:\GoWorckspace\src> go build .\hello\hello.go
open C:\Users\Avetis\AppData\Local\Temp\go-build274908638\b001\exe\a.out.exe: The system cannot find the file specified.

Bounty Edit

A table like this would be helpfull

go env Default path Alternative path windows variable
GOPATH ? c:\dev\golang\ %gopath%
GOBIN ? C:\dev\gobin\bin ?
GOROOT C:\Program Files\Go C:\Program Files\Go ?
GOMOD ? ? "" on my box ?
GOWORK ? ? "" on my box ?

Also as a go beginner i was at first not aware that the source code must be placed in a subfolder src of %gopath% and that the directories pkg and bin are also required.

C:\cygwin64\bin>tree -L 1 %gopath%

c:\dev\golang
├── bin
├── pkg
└── src

Is a folder structure like the one needed for %gopath% (see above) mandatory for other go evn names?

Here is written to set GOBIN to this go env -w GOBIN=C:\Users\yourname\go\bin.

  • But is this also the case if my GOPATH / %gopath% points to c:\dev\golang\
  • Must GOBIN be a subfolder below GOPATH?
like image 390
Avetis Sargsian Avatar asked Dec 18 '25 13:12

Avetis Sargsian


1 Answers

This to me sounds like your anti-virus software (Windows Defender?) is deleting your executable.

See also https://go.dev/doc/faq#virus

When you call go run or go install, the program is built into your temp folder, then run (for go run) or copied to your GOBIN directory (for go install).

The problem is that as soon as the executable is built, it gets deleted by a buggy anti-virus software, thus the next step of running the executable or copying it to another folder, fails with the given error message.

To solve this, you can add your temp folder to the white-list of your anti-virus so it is not scanned. Or you can change the output directories (GOBIN, GOCACHE, GOPATH, GOTMPDIR) to folders that are already on your anti-virus software's white-list.

like image 129
gonutz Avatar answered Dec 21 '25 05:12

gonutz