This is a creation in Article, where the information may have evolved or changed.
Transferred from: http://my.oschina.net/qinhui99/blog/57046
Legend has it that the best development environment for the go language is Apple's Xcode, and I did the test. A friend who needs to compile a go program with C code with CGO, suggests Apple's xcode environment is preferred. Configuring CGO compilation in a Windows environment is cumbersome, as Go's documentation examples are mostly UNIX-based, so some C code cannot be compiled under Windows and requires code changes.
I use the Go language version is the latest release version of Go 1. Download Address: Http://code.google.com/p/go/downloads/list
A link to the official Go installation documentation for Chinese translators:
Http://code.google.com/p/golang-china/wiki/Install. This official installation document feels a bit outdated and is for informational purposes only.
There is also a "learning Go language" Chinese version can refer to: Address: http://www.mikespook.com/learning-go/comment-page-1/#comment-4000
Here are some of the records I configured for the Go language development environment under WinXP:
Configuring Go in Windows environment (required)
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.
(PS: The original author obviously thinks we are a master, so so brief, but I am really a rookie, did not see the original author's meaning, stuck here, fortunately found another article: http://devbbs.doit.com.cn/ Thread-29077-1-1.html, but it's based on Linux but it's very instructive.
It is set to:
Http://devbbs.doit.com.cn/thread-29077-1-1.html
Before installing go, you need to install mercurial, which is the version control tool for Go, which can be added directly through the Ubuntu installer.
Then define the following bash variables for the current user. Typing the CD $HOME under Ubuntu is usually/home/username, then tap Vim. BASHRC, modify. BASHRC
Add the following to this file (my CPU is Intel, so the Goarch is 386, specifically refer to the golang.org description:
goroot= "$HOME/go"
Export Goroot
Goos=linux
Export GOOS
Goarch=386
Export Goarch
gobin= "$HOME/bin"
Export GOBIN
Writing Gobin in Help is optional, but I tried it and must have.
At the end of the. BASHRC, add this line to ensure that the installation is properly compiled.
Path= $PATHGOBIN
In other words, I am now in the situation: Goroot = C:\go, goos=windows, goarch=386)
For example: my machine:
Gopath= c:\go;c:\go\src; F:\WORKSPACE\GOSAMPLE01;
Gobin=c:\go\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 The remote package is downloaded automatically and the packages are compiled and installed.
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:
Package Import "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 first step 3 directly compile 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)) modified to " C.srand (C.uint (i)) "Compile is OK."