[Blockchain Go] Basic syntax

 

 Past articles

[Blockchain go] Install Go and environment variable configuration in windows system

[Blockchain Go] Vscode writing tool and main() function


Table of contents

keywords

  

variable

local variable

global variable

constant

explicit declaration

Implicit declaration


keywords

There are roughly 25 basic type keywords in the Go language:

break

default

func

interface

select

case

defer

go

map

struct

chan

else

goto

package

switch

const

fallthrough

if

range

type

continue

for

import

return

was

Keywords: As an important part of the go language, it is used in various data declarations and data execution.

Keyword Definition:

  •     var and const: declaration of variables and constants
  •     package and import: import
  •     func: used to define functions and methods
  •     return: used to return from a function
  •     defer someCode: execute before the function exits
  •     go: for parallelism
  •     select: used to select different types of communication
  •     interface: used to define an interface
  •     struct: used to define abstract data types
  •     break, case, continue, for, fallthrough, else, if, switch, goto, default flow control
  •     chan is used for channel communication
  •     type is used to declare custom types
  •     map is used to declare custom types
  •     range is used to read slice, map, channel data

  

package main   //包的声明

 package main represents which package the current .go file belongs to, where package is the keyword for declaring packages in the Go language, and main is the name of the package to be declared. package main represents an independently executable program, and every Go application contains a package named main .

variable

package main
import "fmt"
func main(){
	var name string = "hello world"
	name = "Merry Christmas, Mr Lawrence"
	fmt.Println(name)
}

  

Variables are defined using var, the type is written after the variable name, and then assigned

var variable name variable type = value

local variable

package main
import "fmt"

func main(){	
	var Name string = "hello world"
//函数内部声明的变量只能在函数内被使用
	fmt.Println(Name)
}

The variables declared in the function body are called local variables, and the parameters and return value variables of the function belong to local variables. Local variables must be inside a function. In which {} is declared, it can only be accessed in which {}. A local variable does not always exist, it only exists after the function that defines it is called, and the local variable will be destroyed after the function call ends.

global variable

package main
import "fmt"
//外不定义变量 全局变量
var Name string = "hello world"
func main(){	

	fmt.Println(Name)
}

Global variable declaration: A variable declared outside a function is called a global variable. Global variable declarations must start with the var keyword, and the first letter must be capitalized if you want to use a global variable in an external package. Global variables are declared outside the function and can be accessed by the entire package. Global variables can also be accessed across packages if the first letter is capitalized.

constant

explicit declaration

package main
import "fmt"
const age int = 12
var Name string = "hello world"
func main(){	

	fmt.Println(age)
}

Constants are declared with const,      

const const-name const-type = value

The declaration of a constant begins with the keyword const, followed by the constant type and assignment, with no other punctuation at the end of the line. It should be noted that constants must be assigned values ​​when they are defined, but the short variable declaration keyword := cannot be used to define constants.

Implicit declaration

package main
import "fmt"
const age  = 12
var Name string = "hello world"
func main(){	

	fmt.Println(age)
}

 

 

Just cancel the defined type.

const constant name = value

Since Go is a compiled language, constant types can be omitted when defining constants because the compiler can infer the type of a variable based on its value. A constant declaration can specify both a type and a value. If no type is specified explicitly, the type is inferred from the expression on the right.

The scope is divided into local constants and global constants like variables, which will not be explained here. 

Guess you like

Origin blog.csdn.net/m0_62360527/article/details/127444076