This is a creation in
Article, where the information may have evolved or changed.
I do Java development, from the touch of Java began to calculate, has been 8 years, why do you want to learn go language? Front-end time I have been learning jvm,java some of the more underlying things, combing the review, feel can learn other development language, to improve my understanding of Java, stand on the other side of the view of Java. But also afraid of dragonfly water-style learning, not reach the effect, so there is the next article, hope to write down. The history of Go is not discussed here, it is a very background language. The following article content, basically will involve the go language and the Java comparison. let's get started! We salute the predecessors of previous programming and come to a Hello world.
Package main import "FMT" Func Main () { //single-line comment/ * Multiline Comment */ FMT. Println ("Hello, world!")}
The code is divided into three parts
- Package Declaration
- Importing packages
- Function
Package declaration: Go Language Restriction Program Portal go file, must be named Main, otherwise error.
Go run:cannot Run Non-main package
Import Package: The following output statements are used in the FMT package. But the go language has a different place, if the following code does not use the previously imported Bao, will be an error.
# Command-line-arguments./hello_world.go:4: Imported and not used: "Flag"
function: We see the entry of the Go language as a function, no class exists. Func is the key word for the Go Language definition function. The function name is main, and the entry function Convention has to be called Main, otherwise the error:
# Command-line-argumentsruntime.main:call to external function main.mainruntime.main:main.main:not Definedruntime.main:undefined:main.main
Note: In the code we can see the format of single-line comments and multiline comments. we see a curly brace ({) Behind the function, and many people think it's just some programming habit. In fact, the go language syntax requires that the first half of the brace must be followed by a statement, not another line, as follows:
xxx { yyyy}
Otherwise, the error:
# Command-line-arguments./hello_world.go:6: Syntax error:unexpected semicolon or newline before {
elsewhere in the go language, there are requirements for this. let's look at the Java language Hello World.
public class Test {public static void Main (String args[]) { System.out.println ("Hello World!");} }
In fact, Java is very limited, we can not set the package name, he is only the main method is required. Go does not require the Mian method to belong to a class.