Directory
- Go Introduction
- Installation
- Hello World
- Function
- Variable
- Constant
- Visibility rules
- Conclusion
Preface: The recent group to test the water blockchain, the initial plan is set to use Fabirc to get, and fabric of the smart contract is to write with go, take this opportunity to learn to go, this article is from scratch, will go to the basic syntax, structure, function, method, interface, concurrency and so on small demo way to explain, some places will compare Java to deepen memory
Go Introduction
Go also called Golang is developed by Google, in November 2009 Open source of a service-side programming language, go is simple, efficient, support concurrency, in short, worth a look-Wikipedia
Installation
- go environment: Go website Download install is the way next
- IDE: liteide official website Download I use is liteide, download compressed package directly after decompression use, how to use on their own learning is very simple
Hello World
Learning every programming language starts with Hello World, and this is
package mainimport ( "fmt")func main() { fmt.Println("hello world")}
Run output directly in the IDE: Hello World
A. Go file will typically include
- Claim Pack: Package
- Introduction Package: Import
- variable/constant: var/const
- function: func
Declaring packages
Use the PACKGE keyword to declare a package, each Go program consists of a package
Introduction Package
To import the required package using the Import keyword, the go language enforces that the imported package must be used otherwise compiled
import "fmt"import "math"//更简便的写法import ( "fmt" "math")
Function
Use the FUNC keyword to declare a function
Common structure:
func methodName(var1 int,var2 int) int{ ..... return xxx }
Attention:与java不同的地方
- Structure of the entry: variable name in front, type behind, e.g. (var1 int,var2 int)
java是类型在前,变量名在后
- Return value: The return value of the function after the parameter
java中是在方法名之前
一个.go只能声明一个main函数,否则编译不过
helloworld demo 的main函数没有入参也没有返回值,所以可以省略
Variable
Use Var to declare a list of variables, like the function's argument list, type at the end;
var can also be set within the package and within the function;
If the defined variable is not assigned, the initial value is assigned by default:
- Numeric type is 0
- Boolean type is False
- The string is ""
var isfalse boolvar istrue bool = truefunc main() { var num int fmt.Println(num, isfalse, istrue)}
Define multiple and initial values
If the initialization value already exists, the type can be omitted, and the variable will get the type from the initial value.
var isfalse, istrue, x = true, false, "chenmingyu"func main() { fmt.Println(isfalse, istrue, x)}
When two or more variable types are the same, simply write the type of the last variable, such as: x int, y int can be written as x, y int
function, concise assignment statement: = can be in the type of a clear place to replace the Var declaration, can only be used within the function, so the function can only use Var
func main() { var1 := "mingyu" fmt.Println("say hello", var1)}
Constant
Use the CONST keyword to declare constants, such as:
const word = "hello go"func main() { fmt.Println(word)}
return value of the function
The Go language function supports returning any number of return values, such as:
func transform(x, y string) (string, string) { return y, x}func main() { a, b := transform("say", "hello") fmt.Println(a, b)}
Visibility rules
Use the first letter case in go to determine whether the constant, variable, struct, function, interface can be called by an external package
- The first letter lowercase is equivalent to private in Java
- The initial capitalization is equivalent to public in Java
package mainimport ( "fmt" "math")func main() { fmt.Println(math.Abs(12)) //小写报错 undefined: math.abs fmt.Println(math.abs(12))}
Conclusion
First knowledge of Go language series
- [x] Initial go language
- [] first knowledge of Go language: grammar and type
- [] First knowledge of the go language: methods, interfaces and concurrency
Note: This article for Go Environment installation and the installation of the IDE to provide a Web site, download can be installed on its own, liteide use can be self-learning very simple
Learn a new programming language. The most important thing is to write, see three times than your own code again, so start it
Study site: https://tour.go-zh.org
Follow me on the public number
First knowledge of Go language