Home  >  Article  >  Backend Development  >  Is go language a dynamic language?

Is go language a dynamic language?

青灯夜游
青灯夜游Original
2023-01-06 19:18:443345browse

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".

Is go language a dynamic language?

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

## It should be noted that the Go language is different from many programming languages ​​in that it puts the type of the variable after the name of the variable when declaring the variable. The advantage of this is that it can avoid ambiguous declaration forms like in C language, such as int* a, b;. Among them only a is a pointer and b is not. If you want both variables to be pointers, you need to write them separately. In Go, they can and easily be declared as pointer types:

var a, b *int

The basic types of the Go language are:

  • bool

  • string

  • int、int8、int16、int32、int64

  • uint、uint8、uint16、uint32、uint64 , uintptr

  • byte // Alias ​​of uint8

  • rune // Alias ​​of int32 represents a Unicode code

  • float32, float64

  • complex64, complex128

When a variable is declared, the system automatically assigns it the zero value of that type. : int is 0, float is 0.0, bool is false, string is empty string, pointer is nil, etc. All memory in Go is initialized.

The naming rules of variables follow camel naming, that is, the first word is lowercase and the first letter of each new word is capitalized, for example: numShips and startDate.

There are several forms of variable declaration, which are summarized in the following sections.

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:

  • Define the variable and initialize it explicitly.

  • Cannot provide data type.

  • can only be used inside a function.

Like the var form declaration statement, the short variable declaration statement can also be used to declare and initialize a set of variables:

i, j := 0, 1

下面通过一段代码来演示简短格式变量声明的基本样式。

func main() {
   x:=100
   a,s:=1, "abc"
}

因为简洁和灵活的特点,简短变量声明被广泛用于大部分的局部变量的声明和初始化。var 形式的声明语句往往是用于需要显式指定变量类型地方,或者因为变量稍后会被重新赋值而初始值无关紧要的地方。

【相关推荐:Go视频教程编程教学

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn