Go Basic Syntax: Package and Import, Variable, Function, Branch and Loop, Container
Introduction
- Go is an open-source programming language developed by Google. The design goal of Go is to improve the development efficiency of programmers. The syntax of Go is simple, easy to read, and easy to write, suitable for building efficient, reliable, and concise software.
- The syntax of Go is similar to that of C, but some redundant syntax is removed, and some new features are added, such as garbage collection, concurrent programming, etc.
- This article is suitable for readers with a certain programming foundation (recommended for C) to read, mainly introducing the basic syntax of Go language and helping readers quickly get started with Go language.
- Zero-based users can try to read the Go official documentation or Runoob tutorial.
Basic Program
A simple Go program consists of package declaration, package import, function declaration, and function body. The entry point of a Go program is the main
function, and the package where the main
function is located must be the main
package.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Package
A package is the basic organizational unit of Go language, and each Go file belongs to a package. The declaration of a package is implemented through the package
keyword. The package name and folder name can be inconsistent, but it is recommended to keep them consistent.
The syntax of declaring a package is as follows:
Import
The import syntax of Go language is as follows:
import "fmt"
Multi-line import
import (
"fmt"
"os"
)
Import standard Go packages from git repository
import "github.com/cloudwego/kitex"
After importing, you can use functions, variables, etc. in the package.
import (
"fmt",
"github.com/cloudwego/kitex"
)
func main() {
fmt.Println("Hello, World!")
s := kitex.NewServer()
}
package main
Variable
The variable declaration format of Go language is as follows (type postposition):
var variable_name type
For example
var a int
var b string
The variable declaration of Go language can also initialize variables while using the var
keyword, the format is as follows:
var variable_name type = expression
The variable declaration of Go language can also use the :=
operator to initialize variables, the format is as follows: (can only be used in functions)
variable_name := expression
Function
The function declaration format of Go language is as follows:
func function_name(parameter_list) (return_value_list) {
function body
}
For example
func add(a int, b int) int {
return a + b
}
Branch and Loop
Go language has if
and switch
two branch statements, and for
and range
two loop statements.
if-else
if condition {
// do something
} else {
// do something
}
switch
switch condition {
case 1:
// do something
case 2:
// do something
default:
// do something
}
for
for i := 0; i < 10; i++ {
// do something
}
range
for index, value := range array {
// do something
}
Container
Go language has arrays, slices, dictionaries, structures, etc.
Array
var arr [5]int
Slice (can be understood as a dynamic array, and the slice is a reference type, the array is a value type, the slice is used in more scenarios than the array)
var slice []int
Dictionary
var dict map[string]int
Structure
type Person struct {
Name string
Age int
}
Summary
This article introduces the basic syntax of Go language, including packages, variables, functions, branches and loops, containers, etc. I hope it will be helpful to everyone.