Home >Backend Development >Golang >Is go language a dynamic language?
Go is not a dynamic language. The go language is a statically strongly typed, compiled, concurrent programming language with garbage collection capabilities developed by Google. Its variables (variables) have clear types, and the compiler will also check the correctness of the variable types; therefore The data type must be declared before using the variable, the syntax is "var variable name variable type".
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
go is not a dynamic language, but a static language.
Go (also known as Golang) is a statically strongly typed, compiled, concurrent, and garbage-collected open source programming language developed by Google's Robert Griesemer, Rob Pike, and Ken Thompson.
Introduction to dynamic language and static language
Dynamic language (weakly typed language)Yes In a language where the data type is determined at runtime, the variable does not need to be declared before being used. Usually the value of the variable is the type of the value to which it is assigned. Such as Php, Asp, JavaScript, Python, Perl, etc.
var s ="hello"; var i = 0; var b = true;
Static language (strongly typed language) is a language in which the data type of the variable can be determined at compile time. Most static languages require Data types must be declared before using variables. Such as Java, C, C, C#, Go, etc.
String s="hello"; //String 类型的变量 boolean b=true; //boolean 类型的变量 int i=0; //int 类型的变量
Weakly typed language is a language in which data types can be ignored. It is the opposite of a strongly typed language, where a variable can be assigned values of different data types. A variable's type is determined by its context, which is more efficient.
Strongly typed language is a language that must forcefully determine the data type. Once a variable is assigned a certain data type, if it is not forced to convert, then it It is always this data type. The type of a variable is determined when it is declared, which is safer.
Declaration of Golang variables (using the var keyword)
Go language is a statically typed language, so variables ( variable) has a clear type, and the compiler will also check the correctness of the variable type. In mathematical concepts, a variable represents a number that has no fixed value and can be changed. But from a computer system implementation perspective, a variable is one or more segments of memory used to store data.
The general form of declaring variables is to use the var keyword:
var name type
var
is the keyword for declaring variables
name
is the name of the variable
type
is the type of the variable
var a, b *intThe basic types of the Go language are:
Standard format
The standard format of variable declaration in Go language is:var 变量名 变量类型The variable declaration starts with the keyword var, followed by the variable type, No semicolon is required at the end of the line.
Batch format
Do you think it’s cumbersome to use var to declare variables in each line? It doesn't matter, there is another way to define variables for lazy people:var ( a int b string c []float32 d func() bool e struct { x int } )Using the keyword var and brackets, a set of variable definitions can be put together.
Short format
In addition to the var keyword, you can also use shorter variable definition and initialization syntax.名字 := 表达式It should be noted that the short variable declaration has the following restrictions:
i, j := 0, 1
下面通过一段代码来演示简短格式变量声明的基本样式。
func main() { x:=100 a,s:=1, "abc" }
因为简洁和灵活的特点,简短变量声明被广泛用于大部分的局部变量的声明和初始化。var 形式的声明语句往往是用于需要显式指定变量类型地方,或者因为变量稍后会被重新赋值而初始值无关紧要的地方。
The above is the detailed content of Is go language a dynamic language?. For more information, please follow other related articles on the PHP Chinese website!