суббота, 3 июня 2017 г.

How to install Go in Cygwin.

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Prerequisites: Cygwin and Git are already installed in your Windows 7.

0. Read "Installing Go from source". It's optional. :)
1. Download go1.8.3.windows-386.zip
2. Unzip go1.8.3.windows-386.zip into в D:\go1.8.3.windows-386
3. Start Windows console:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

D:\Console2>set GOROOT=D:\go1.8.3.windows-386\go

D:\Console2>echo %GOROOT%
D:\go1.8.3.windows-386\go

D:\Console2>set Path=%Path%;%GOROOT%\bin

D:\Console2>echo %PATH%
C:\Windows\system32;C:\Windows;...;D:\Git\cmd;D:\go1.8.3.windows-386\go\bin

D:\Console2>where go
D:\go1.8.3.windows-386\go\bin\go.exe
4. Start cygwin:
Vadim@Vadim-PC ~
$ which git
/cygdrive/d/Git/cmd/git

Vadim@Vadim-PC ~
$ git clone https://go.googlesource.com/go
Cloning into 'go'...
remote: Sending approximately 148.89 MiB ...
remote: Total 305345 (delta 240031), reused 305345 (delta 240031)
Receiving objects: 100% (305345/305345), 148.89 MiB | 1.06 MiB/s, done.
Resolving deltas: 100% (240031/240031), done.
Checking out files: 100% (6627/6627), done.

Vadim@Vadim-PC ~
$ cd go

Vadim@Vadim-PC ~/go
$ git checkout go1.8.3
Note: checking out 'go1.8.3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b 

HEAD is now at 352996a381... [release-branch.go1.8] go1.8.3

Vadim@Vadim-PC ~/go
$ git checkout master
Previous HEAD position was 352996a381... [release-branch.go1.8] go1.8.3
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

Vadim@Vadim-PC ~/go
$ cd src

Vadim@Vadim-PC ~/go/src
$ ./make.bat
##### Building Go bootstrap tool.
cmd/dist
ERROR: Cannot find C:\Users\Vadim\Go1.4\bin\go.exe
"Set GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4."

Vadim@Vadim-PC ~/go/src
$ GOROOT_BOOTSTRAP=D:\\go1.8.3.windows-386\\go

Vadim@Vadim-PC ~/go/src
$ echo $GOROOT_BOOTSTRAP
D:\go1.8.3.windows-386\go
5. Add command "set GOROOT_BOOTSTRAP=D:\go1.8.3.windows-386\go" into file go/src/make.bat:
...
echo ##### Building Go bootstrap tool.
echo cmd/dist
if not exist ..\bin\tool mkdir ..\bin\tool
if "x%GOROOT_BOOTSTRAP%"=="x" set GOROOT_BOOTSTRAP=%HOMEDRIVE%%HOMEPATH%\Go1.4

set GOROOT_BOOTSTRAP=D:\go1.8.3.windows-386\go

if not exist "%GOROOT_BOOTSTRAP%\bin\go.exe" goto bootstrapfail
...
6. Try to execute make.bat again:
Vadim@Vadim-PC ~/go/src
$ ./make.bat
##### Building Go bootstrap tool.
cmd/dist

##### Building Go toolchain using D:\go1.8.3.windows-386\go.
bootstrap/cmd/internal/objabi
bootstrap/cmd/internal/src
bootstrap/cmd/internal/dwarf
bootstrap/cmd/internal/sys

...skipped...

cmd/compile/internal/arm
cmd/compile/internal/arm64
cmd/compile/internal/mips
cmd/compile/internal/mips64
cmd/compile/internal/ppc64
cmd/compile/internal/s390x
cmd/compile/internal/x86
cmd/compile

Vadim@Vadim-PC ~/go/src
$ export PATH=$PATH:$HOME/go/bin

Vadim@Vadim-PC ~/go/src
$ which go
/home/Vadim/go/bin/go

Vadim@Vadim-PC ~/go/src
$ cd $HOME

Vadim@Vadim-PC ~
$ mkdir $HOME/mygo

Vadim@Vadim-PC ~
$ mkdir $HOME/mygo/src

Vadim@Vadim-PC ~
$ mkdir $HOME/mygo/src/hello

Vadim@Vadim-PC ~
$ cd mygo/src/hello

Vadim@Vadim-PC ~/mygo/src/hello
$ pwd
/home/Vadim/mygo/src/hello

Vadim@Vadim-PC ~/mygo/src/hello
$ touch hello.go
7. Check that Go is installed correctly by building a simple program.
Create a file named hello.go and put the following program in it:
package main

import "fmt"

func main() {
 fmt.Printf("Hello, world.\n")
}
8. Set up environmental variables:
Vadim@Vadim-PC ~/mygo/src/hello
$ export GOROOT=D:\\cygwin64\\home\\Vadim\\go

Vadim@Vadim-PC ~/mygo/src/hello
$ echo $GOROOT
D:\cygwin64\home\Vadim\go

Vadim@Vadim-PC ~/mygo/src/hello
$ export GOPATH=D:\\cygwin64\\home\\Vadim\\mygo

Vadim@Vadim-PC ~/mygo/src/hello
$ echo $GOPATH
D:\cygwin64\home\Vadim\mygo
9. Build and run it with the go tool:
Vadim@Vadim-PC ~/mygo/src/hello
$ go build

Vadim@Vadim-PC ~/mygo/src/hello
$ ./hello
Hello, world.

Vadim@Vadim-PC ~/mygo/src/hello
$
If you see the "Hello, world" message then Go is installed correctly.