This is a creation in Article, where the information may have evolved or changed.
Because of the encounter go, the charm of go to be impressed, slowly will happen the following story
Installation of Go under Ubuntu
There are several ways to install the go, you can download the corresponding files according to your own system version. Share my installation, the author system version is Linux (Ubuntu) 64-bit, will be based on Liunx (Ubuntu) to share.
- Download Go's source code
- Unzip the source---> Custom folders
- Setting environment variables
- Goroot:
export GOROOT=/home/ubuntu/go/ , the Go source folder location
- Gopath:
export GOPATH=/home/ubuntu/workspace/ , go code storage location
- PATH:
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin , setting the system environment variable
Note: Under Description, modify variables in the response configuration file based on the type of local script, for example: My is zsh, I need to .zshrc modify in the
After doing this, we can type in the command line go to verify that the installation is successful and similar to the java command.
Small trial Sledgehammer
After installing go, can we workspace write our go code in? Yes, but we have to manage our program structure, so we workspace need three folders:
- Bin: After compiling the executable file
- PKG: App Package
- SRC: Application Source (the code we write)
Create the project in the SRC directory, and jvmgo then jvmgo create subdirectories under the file ch01 , my project directory diagram is as follows:
Workspace.jpg
Simple implementation of command-line settings, the effect is as follows:
Result.jpg
Command explanation:
- Go BUILD: Compile code that generates the same executable file as the file by default
- CH01-VERSION:CH01, compiled executable file,-version, pre-defined commands.
- CH01-HELP:CH01, compiled executable file,-HELP, pre-defined commands.
Implementation ideas:
- Write command-line tool files that define command-line information such as: Terminal.go
flagpre-defined commands with modules
- Write the main function test execution
Create a Terminal.go file under the/ch01/folder, define command line information, and pre-order commands:
//define command line information, termial struct type Terminal struct {helpflag bool//-HELP versionflag bool//-version Cpoption string//-CP class string//Specify class args []string//parameter}//predefined command func parseterminal () *terminal {//define a Terminal struct, because the pointer exists C,c++,go, so Yong "&terminal{}", give the address to Terminal Terminal: = &terminal{} /* Define command Line Parameters */flag. Usage = Printusage flag. Boolvar (&terminal.helpflag, "help", False, "Print help message") flag. Boolvar (&terminal.helpflag, "?", False, "Print help message") flag. Boolvar (&terminal.versionflag, "version", False, "print version and Exit") flag. Stringvar (&terminal.cpoption, "Classpath", "" "," classpath ") flag. Stringvar (&terminal.cpoption, "CP", "" "," classpath ")//command line parameters parse flag. Parse () args: = flag. Args () If Len (args) > 0 {terminal.class = args[0] Terminal.args = args[1:]} return terminal}
Create the Main.go file under the/ch01/file and test:
package mainimport "fmt"func main() { terminal := parseTerminal() if terminal.versionFlag { fmt.Println("version 0.0.1") } else if terminal.helpFlag || terminal.class == "" { printUsage() } else { startJVM(terminal) }}func startJVM(terminal *Terminal) { fmt.Printf("classpath:%s class:%s args:%v\n", terminal.cpOption, terminal.class, terminal.args)}
Note: The above code is pseudo-code bar, source file also please shift source
Reference documents:
- Write your own Java Virtual machine
- Https://github.com/astaxie/build-web-application-with-golang