Setting up the environment

Hi,

I have no experience programming go, however i did some PHP for serveral years. I did some research and Go looks very interesting to learn.

As a webdeveloper there was no ‘hard’ setup for any environment, just write some code and upload the file, so installing Go is kind of new for me.

  • I installed succesfuly GoLang and the Go Server is running on my machine.
  • I created a new directory for the src, pkg and bin.
  • I changed the GOBIN en GOPATH
  • I created a hello.go file

But i cannot run or build the code.

[code]
C:\Users\Bas>go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Bas\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\gocode
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\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\Bas\AppData\Local\Temp\go-build767200714=/tmp/go-build -gno-record-gcc-switches

C:\Users\Bas>go run d:\gocode\src\hello
package d:/gocode/src/hello: cannot find package “d:/gocode/src/hello” in any of:
C:\Go\src\d:\gocode\src\hello (from $GOROOT)
D:\gocode\src\d:\gocode\src\hello (from $GOPATH)

C:\Users\Bas>[/code]

Any suggestions ? Thanks!

Hi

I wouldn’t put any source in $GOPATH. I would put it anywhere in my profile/homedirectory and if it is a simple one/two file program with no external dependencies would i run it and and build it with for example go run hello.go or build it with go build -o hello hello.go

On the otherhand if it is a program with external dependencies would I make a folder and init a module inside it:

C:> mkdir hello
C:> cd hello
C:> go mod init github.com/johandalabacka/hello

You could really name your module anything but If you uses github it will be best to name it after the repo. Modules are new in go 10.11 and is the future. Sadly if you google on the internet will you find a lot of examples of people putting stuff into the GOPATH. Read more about modules here: https://github.com/golang/go/wiki/Modules

What is “the Go Server”? Can you share the code of your hello.go, maybe in a Gist?

Hi,

A bit embarrising but i think i installed the packages and compiler (?) on my C drive, while the gocode dir is located on D. Could this be the problem ?

I preffer the current location: D > gocode > src, pkg and bin, because it is very clean and easy to navigate.

Should I re-install GoLang too the D drive ? I do not use Gitbub btw.

package main

import "fmt"

func main() {
	fmt.Printf("hello, world\n")
}

It works!

C:\Users\Bas>d:

D:\>cd gocode

D:\gocode>cd testProject

D:\gocode\TestProject>go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Bas\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Bas\go
set GOPROXY=
set GORACE=
set GOROOT=d:\Go
set GOTMPDIR=
set GOTOOLDIR=d:\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\Bas\AppData\Local\Temp\go-build118081226=/tmp/go-build -gno-record-gcc-switches

D:\gocode\TestProject>set GOPATH=d:\gocode\testProject

D:\gocode\TestProject>set GOBIN=d:\gocode\testProject\bin

D:\gocode\TestProject>go env
set GOARCH=amd64
set GOBIN=d:\gocode\testProject\bin
set GOCACHE=C:\Users\Bas\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=d:\gocode\testProject
set GOPROXY=
set GORACE=
set GOROOT=d:\Go
set GOTMPDIR=
set GOTOOLDIR=d:\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\Bas\AppData\Local\Temp\go-build132766326=/tmp/go-build -gno-record-gcc-switches

D:\gocode\TestProject>go run src/main/hello.go
hello world

D:\gocode\TestProject>
1 Like

Nice. However I still would recommend you to look into go modules :slight_smile:

before build code, you should set your GOPATH environment as following:

set GOPATH=D:\gocode

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.