This is a creation in Article, where the information may have evolved or changed.
1. Download the zip file for go. And be sure to extract the files into the C:\go directory.
2. Configure advanced environment variables for Windows. Including: Goroot, GOOS, GOBIN, Goarch. and add C:\go\bin to the PATH variable. So that you can run the GO command directly from the command line.
For example: my machine:
Set goarch=386
Set Goos=windows
Set Goroot=c:\go
Set Gobin=%goroot%\bin
Set gopath=%goroot%; F:\WORKSPACE\GOSAMPLE01;
GOPATH=%GOROOT%;%GOROOT%\SRC; F:\WORKSPACE\GOSAMPLE01;
Gobin=%goroot%\bin; F:\workspace\goSample01\bin;
Among them, C:\go is the installation path of Go;
F:\WORKSPACE\GOSAMPLE01 is my project directory for the Go Language project;
F:\workspace\goSample01\bin is the executable file path under the project directory of the Go Language project;
3, after completing the environment variable configuration, open a command line window, enter go directly, and then enter to see if there is a go help information. If it does, then the basic environment of GO is OK.
Note: This basic environment does not include development tools, nor does it directly compile a go program with C code.
4, (optional) in order to support the import remote package, it is best to install a gomingw. Download Address: Http://code.google.com/p/gomingw/downloads/list. If the following is a compressed package, please unzip it to C drive. For example, C:\gowin-env. There is a console.bat in the future using go get environment. Example: There is a file a.go, inside import (
"FMT"
"Github.com/astaxie/beedb"
_ "Github.com/ziutek/mymysql/godrv"
In order to compile the A.go file, you need to start Console.bat, and then in the Command Line window, go to c:\go\src directory and execute Go getgithub.com/astaxie/beedb
Go get github.com/ziutek/mymysql/godrv.
Go will automatically download the remote package and compile and install the packages.
Configure Goclipse (optional)
(If you don't like the Eclipse development tools, skip this configuration.) )
1. Download and install the Goclipse plugin. Goclipse is a plugin for go language for Eclipse, download address: http://code.google.com/p/goclipse/
2. Start Eclipse and create a go project. Then write one of the simplest helloworld.go files and run them. The code is as follows:
Packagemainimport "FMT" Func Main () {FMT. Printf ("Hello, World")}
Configure Gocode (optional)
If you do not need go grammar assist and Eclipse (press alt+/) to eject the Go Language auto-assist feature, please skip this configuration.
1, download Gocode zip file, unzip and put in the Go Bin directory.
2. Download and install the Git software. And configure Git's execution path in path. such as C:\git\bin
3. Execute at command line: Go build. \gocode. If all goes well, then a Gocode.exe file will be compiled and generated in the Go Bin directory. If the compilation fails, then go to step 4th.
4, if the 3rd step directly compiled Gocode source file success, then go straight to the 5th step. Otherwise, you will need to download the Gocode source file from git and then compile it. Execute at the command line: Go get-u github.com/nsf/gocode. The Gocode.exe file is generated.
5, in the Goclipse plug-in to specify the path of Gocode. You can call Gocode in Elcipse to help write the code.
From the perspective of development tools, go language is not mature enough, development tools are not perfect, need to be improved.
Download Go-tour Tutorial source code (optional)
Google has a tutorial on running the Go language online (http://tour.golang.org/#2), very good. Support to run most of the Go program directly on the web, friends who want to know the source code of this tutorial can be obtained by the following ways. If you are not interested, you can skip this step.
1. Download and install mercurial software.
2. At the command line, enter:
HG Clone http://qinhui99@code.google.com/r/qinhui99-go-tour/
http://qinhui99@code.google.com/r/qinhui99-go-tour/This URL is a clone of my go-tour source code from Google. Used as a test. If you change the HTTP to HTTPS protocol, the download will fail. Don't understand.
Compile the go file with call C code (optional)
1, in order to compile a go program with C code under Windows, you first need to download and install MinGW or Cygwin.
2, the preferred installation MinGW. After installing MinGW, remember to set the MinGW installation directory \ Bin path in the PATH environment variable so that you can call GCC directly under the DOS window.
3, download a gowin-env. Download Address: gowin-env. Unzip to a directory after downloading, for example: C:\gowin-env. Then, edit Go-env.bat. Configure the relevant go parameters. For example, my configuration is:
Set goarch=386
Set Goos=windows
Set Goroot=c:\go
Set Gobin=%goroot%\bin
Set gopath=%goroot%; F:\WORKSPACE\GOSAMPLE01;
Once the Go-env.bat is set, you can click Console.bat to start compiling and running the window.
4. Write a go program with C code. For example, Testc.go
5. Compiling
For example:
Go Build-compiler gccgo test_c.go
Run a go file that calls C code (optional)
1, Testc.go.
Create the Rand directory and create the testc.go inside Rand. The code is as follows:
Package Rand
/*
//
#include <stdio.h>
*/
Import "C"
Func Printhello () {
C.puts (C.cstring ("Hello, world\n"))
}
2, A.go
Create A.go under Rand. The code is as follows:
Package Rand
Import "FMT"
Func SayHello (name string) {
Fmt. PRINTLN (name)
}
3, Test_import.go
Create Test_import.go at the top level of Rand. The code is as follows:
Package Main
Import "./rand"
Func Main () {
Rand. SayHello ("Tom")
Rand. Printhello ()
}
4. Running Test_import.go
Go Run test_import.go
When testing a few other C code, found that the Windows version of CGO also some compilation problems, the same code transferred to the Apple Xcode under no problem. Then finally found the reason, there are some examples of UNIX platform, and under the Windows platform, the method name and parameters need to be adjusted.
For example, the following code compiles a heap of errors under Windows.
Package Rand
/*
#include <stdlib.h>
*/
Import "C"
Func Random () int {
return int (C.random ())
}
Func Seed (i int) {
C.srandom (C.uint (i))
}
You need to change the return int (C.random ()) to "return Int (C.rand ())"
C.srandom (C.uint (i)) is modified to "C.srand (C.uint (i))" and the compilation is OK.