Write programs in go language under Windows

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Linux, Google's go language is easy to install, it is also very cool, a few lines of code can achieve a very powerful function.
Now the problem is I want to play under windows ...
In fact, Windows is not troublesome, specifically see below.

First, install the Go language:
1, installation MinGW (https://bitbucket.org/jpoirier/go_mingw/downloads)
2. Download the source code
Enter C:\MinGW, double-click Mintty to open the terminal window;
Execute "HG clone-u release https://go.googlecode.com/hg//c/go" download source code;
3. Compiling the source code
Execute "cd/c/go/src" into src directory, execute "./all.bash" to compile;
4. Setting Environment variables
After compiling, the binaries are generated under C:\go\bin, and the "C:\go\bin" is added to the path. ;

Second, write the Go code:
Files: test.go
The code is as follows:

Package Main

Import "FMT"

Func Main () {
Fmt. Println ("Test")
}

Third, generate executable files (in my machine, for example, refer to the official website documentation):
Compilation: 8g-o test.8 Test.go
Link: 8l-o test.exe test.8
Executing the test.exe will output:

Test

Iv. batch generation of executable files

If you write a lot of test code, every time you have to input two times command, feeling very inconvenient.
So I decided to write a script that would automatically traverse all the files in the current directory that end with ". Go", compile the file, generate the target file, link to the executable file, and then delete the target file. This script is written in the previous article (http://www.cnblogs.com/MikeZhang/archive/2012/01/17/2324567.html) that generated the makefile principle, limited functionality, Suitable for writing test code.
Here is the code (Python script):

1 '''
2 File:compileGo.py
3 Author:mike
4 e-mail:mike_zhang@live.com
5 '''
6 ImportOs
7
8Srcsuffix ='. Go'
9Dstsuffix ='. EXE'
TenCmdcompile ="8g"
OneCmdlink ="8l"
A
-Flist = []
- forDirpath,dirnames,filenamesinchOs.walk ('.'):
the forFileinchFileNames:
-name,extension = os.path.splitext (file)
- ifExtension = = Srcsuffix:
-Flist.append (name)
+Tmpname = name +'. 8' #temp File
-Strcompile ='%s-o%s%s'% (Cmdcompile,tmpname,file)
+ PrintStrcompile
AOs.popen (Strcompile)#Compile
atStrlink ='%s-o%s%s'% (Cmdlink,name+dstsuffix,tmpname)
- PrintStrlink
-Os.popen (Strlink)#Link
-Os.remove (Tmpname)#Remove Temp file
- Break #Only search for the current directory

All right, that's it, I hope it helps you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.