This is a creation in Article, where the information may have evolved or changed.
First go lang program--hello world
Package Mainimport "FMT" Func Main () {p ("Rocky")}/* block comment */func p (name string) {FMT. Println ("Hello," + name)}//single line comment//func p () {//FMT. Println ("Hello world!") // }
Some simple grammatical features
The simple code above uses some of the following features of Go lang:
The first line of the code is the package; The above example, a program that runs separately using a single file must be placed under packages main, otherwise the go run runs with an error: "Go run:cannot run Non-main"
Import package name must be quoted, multiple package names can use multiple import, you can enclose the package name in quotation marks, connect with a newline character, and wrap the parentheses around the outermost layer;
Import cannot reference a package that is not used in the code, otherwise the compilation will error: "Imported and not Used:xxx"
"{" must follow the code and not appear in the new line;
Main as a program entry, can not carry parameters, command line parameters using the OS. The args variable gets;
Note there are block comments/* */and single-line comments//two kinds;
Define the func format: Func func_name (input parameter) (return parameter--can be omitted) {};
Func definition does not support overloading (overload)
"The Pit" in Hello World
Don't underestimate the above lines of code, a lot of pits:
Package is not main, resulting in a failure to run
Import FMT does not use double quotes "" Package name, resulting in inability to run
Understanding and use of Go run/go install/go build, etc.