Install a configuration instance using the Go language write program under Windows _golang

Source: Internet
Author: User
Tags python script

Linux, Google's go language is easy to install, it is also good to use, a few lines of code can achieve very powerful functions.
The problem now is that I want to play under windows ...
In fact, Windows is not trouble, 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;
The implementation of "HG Clone-u release https://go.googlecode.com/hg//c/go" download source code;
3. Compiling source code
Execute "cd/c/go/src" into the src directory, execute "./all.bash" to compile;
4. Set environment variable
When the compilation is complete, a binary file is generated under C:\go\bin and "C:\go\bin" is added to the path. ;

Second, write go code:

File: Test.go
The code is as follows:

Copy Code code as follows:

Package Main

Import "FMT"

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

Iii. Generate executable Files (take my machine as an example, specific reference to the official website documents):
Compiling: 8g-o test.8 test.go
Link: 8l-o test.exe test.8
Execute test.exe, Output:

Test

Iv. batch generation of executable files

If you write a lot of test code, each time you have to enter the command two times, it is very inconvenient.
So I decided to write a script that automatically iterates through all the files in the current directory that end with ". Go", compiles the files, generates the target files, links them to executable files, and deletes the target files. This script is written in the previous article (http://www.jb51.net/article/61951.htm) generated in the makefile principle, the function is limited, suitable for writing test code when used.
Here's the code (Python script):

Copy Code code as follows:

'''
File:compileGo.py
Author:mike
E-mail:mike_zhang@live.com
'''
Import OS

Srcsuffix = '. Go '
Dstsuffix = '. exe '
Cmdcompile = "8g"
Cmdlink = "8l"

Flist = []
For dirpath,dirnames,filenames in Os.walk ('. '):
For file in FileNames:
name,extension = os.path.splitext (file)
if extension = = Srcsuffix:
Flist.append (name)
Tmpname = name + '. 8 ' # temp file
Strcompile = '%s-o%s '% (cmdcompile,tmpname,file)
Print Strcompile
Os.popen (strcompile) # Compile
Strlink = '%s-o%s '% (cmdlink,name+dstsuffix,tmpname)
Print Strlink
Os.popen (strlink) # link
Os.remove (tmpname) # Remove Temp file
# Search the current directory


Well, that's all, I hope to help 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.