This is a creation in Article, where the information may have evolved or changed.
2.2 Run
2.2.1 Hello World Instance
hello.go code:
Package Mainimport "FMT" Func Main () {FMT. Printf ("Hello, world; World/n ")}
compile code:
$8g Hello.go
8g is the compiler for the i386 directive, and its output file is hello.8 . where ' 8 ' indicates that the file is an output file of the I386 directive. In the case of AMD64 and arm processors, the suffix is ' 6 ' and ' 5 ' . In other words, if you are using a AMD64 processor, you should compile with the 6g command and the output file is hello.6 .
Link Code:
$8l hello.8
To run the program:
$hello. Out
or use
$8l-o Hello.exe hello.8
Direct Output hello.exe program
If you are compiling a more complex process, you may need to use Makefile . I'm used to using Rake to compile code, and then I need to use rakefile.
Rakefile File instance:
# # Go Language project Build Rakefile # #处理扩展名def ext (FN, newext) fn.sub (//.[ ^.] +$/, Newext) end# with platform-related constants o=8g_as= "#{o}a" g_cc= "#{o}c" g_gc= "#{o}g" g_ld= "#{o}l" g_gpk= "Gopack" g_fmt= "GOFMT" desc "Default task ( Compile). "task:d Efault = = [: Compile] #项目名称TARG =" Gojson "srcdir=". " Bindir= "." Objdir= "." Outdir= "." #源代码 #files = filelist[' reader.go ']files = filelist[' writer.go ', ' reader.go '] #不处理的文件 #srcfiles.exclude ("test") Srcfiles=files.collect {|fn| "#{srcdir}/#{fn}"} #目标文件OBJFILES = files.collect {|fn|filename=ext (FN, ". 8") "#{objdir}/#{filename}"}desc "Compile the project source code, and generate the target file "Task:compile doputs" to start compiling project source code ... "#sh" #{g_gc}-o #{objfiles} #{srcfiles} "Files.each do |fn|srcfile=" #{srcdir} /#{FN} "Filename=ext (FN,". 8 ") objfile=" #{objdir}/#{filename} "sh" #{g_gc}-o #{objfile} #{srcfile} "end End desc" link the target file, and generate the executable "file" link "+: Compile do |t|puts" Start link destination file ... "#sh" #{g_ld}-o #{targ}.exe #{objfiles} "Files.each do |fn|file Name=ext (FN, ". 8") objfile= "#{objdir}/#{filename}" Fileexe=ext (FN, ". exe") outfile= "#{outdir}/#{fileexe}" sh "#{g_ld} -O #{oUtfile} #{objfile} "End Enddesc" run the generated "Task:run" + ["link"] do sh "#{targ}.exe" end# "Build package file" file "#{targ}.a" +: Comp Ile doputs "Start Build Package file ..." sh "#{g_gpk} CRG #{targ}.a #{targ}.8" End desc "Build package File" Task:p kg + = ["#{targ}.a"] doenddesc "build the project. .." Task:build =>["link"] doputs "Build project End" End DESC "format source code" file "FMT" do |t|puts "start format File ..." Files.each do |fn|filename=ext (FN, ". 8") sh "#{g_fmt}-W #{fn}" End Enddesc "Delete the generated temporary file" Task:clean do rm_f filelist[' *.8 '] dir[' *~ '].each {|fn| RM_F fn}enddesc "Delete all generated files" Task:clobber = [: Clean] do rm_f filelist[' *.a ', ' *.exe ']end
Instructions for use:
The name of the source file that needs to be modified,
FILES = filelist[' writer.go ', ' reader.go ']
Add the list of code files you want to compile.
$rake Compile// compile code
$rake link// linking code
$rake Run// compile code
$rake clean// Delete generated temporary files
$rake clobber// Delete all generated files
$rake clobber// Delete all generated files
$rake FMT// format source code
2.2.2 Configuring the development environment
Go source code files are UTF-8 encoded files, and all current text editors are basically supported. In the Go release package, there are code highlighting controls for common editors such as Vim, Emacs, and other editors. I'm using the EditPlus editor, and here's an example of EditPlus to configure the Go development environment.
Compile the configuration:
Link configuration:
Run Configuration:
Code interface:
Tips:
EditPlus Tool Shortcut: Ctrl+1 compilation
Ctrl+2 Links
Ctrl+3 Run