-1

I'm trying to cross-compile an application I've developed. but i am getting this error:

error:

GOOS=linux : The term 'GOOS=linux' is not recognized as a name
cmdlet, function, script file, or operable program. check the
spelling of the name or, if a path was included, see if the
path is correct and try again.

I'm trying to compile with the following code:

 GOOS=linux GOARCH=amd64 go build main.go

my GOOS variable seems to be set when I run the go env command, this appears:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\apoi\AppData\Local\go-build
set GOENV=C:\Users\apoi\AppData\Roaming\go\env
set GOEXE=
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\apoi\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=linux
set GOPATH=C:\Users\apoi\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.19.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\apoi\Desktop\azure-function-curso\go.mod
set GOWORK=
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=-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\apoi\AppData\Local\Temp\go-build584110605=/tmp/go-build -gno-record-gcc-switches

I've also tried running my variables separately, all through the powershell of visual studio code. The attempt I made was this:

set GOARCH=amd64
set GOOS=linux
go tool dist install -v pkg/runtime
go install -v -a std

2 Answers2

1

In PowerShell, environment variables live in the $Env namespace, and are set like this:

$Env:GOOS = 'linux'
$Env:GOARCH = 'amd64'

Also, I am not aware of a simple way to change the environment for only a single run of a command the same way the POSIX sh allows with the

VAR=value command

syntax. You'd probably have to start a separate sub-shell for this, something like this (totally untested) code:

pwsh -Command { $Env:GOOS = 'linux' ; $Env:GOARCH = 'amd64' ; go build main.go }
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
0

If you're encountering errors when using the terminal in VS Code or PowerShell to execute Go code, try using Git Bash instead. Git Bash may be able to provide you with the result you need.

Akash
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34381428) – user16217248 May 18 '23 at 01:17