当前位置:网站首页>Go data types (II) overview of data types supported by go and Boolean types
Go data types (II) overview of data types supported by go and Boolean types
2022-06-23 08:17:00 【weixin_ fifty-nine million two hundred and eighty-four thousand】
Go Supported data types
Go The language has built-in support for these basic data types :
- Boolean type :bool
- integer :int8、byte、int16、int、uint、uintptr etc.
- Floating point type :float32、float64
- Plural type :complex64、complex128
- character string :string
- Character type :rune
- Wrong type :error
Besides ,Go The language also supports the following composite types :
- The pointer (pointer)
- Array (array)
- section (slice)
- Dictionaries (map)
- passageway (chan)
- Structure (struct)
- Interface (interface)
Unlike other static languages ,Go Added a new channel type , This type is mainly used for communication between different coroutines during concurrent programming , Later on Go Language concurrent programming will be introduced in detail .
Structs are similar to classes in object-oriented programming languages (class),Go Continue to use C This compound type of language , Instead of introducing a separate class concept like traditional object-oriented programming ,Go The language also presents interfaces as a single type , Later on Go The use of these two types will be introduced in detail during object-oriented programming .
Boolean type
Go The Boolean types in this language are similar to those in other mainstream programming languages , Type keyword is bool, Can be assigned and can only be assigned to predefined constants true and false. The sample code is as follows :
var v1 bool
v1 = true
v2 := (1 == 2) // v2 It will also be deduced as bool type Go It's a strongly typed language , Once the variable type is determined , You cannot assign values of other types to this variable , therefore , Boolean types cannot accept assignments of other types , Automatic or forced type conversions are not supported . The following examples are some of the wrong uses , Causes a compilation error :
var b bool
b = 1 // Compile error
b = bool(1) // Compile error However, the boolean result calculated by the expression can be assigned to Go Boolean type variable :
var b bool
b = (1!=0) // Compile correctly
fmt.Println("Result:", b) // The result is Result: true Besides , Due to strong typing ,Go When a language judges whether a Boolean value is true or false , There are strict restrictions on the types of values , stay PHP In this weakly typed language , The following values are used in Boolean judgment ( Use non strict == Comparison symbol ) Will be considered as false(JavaScript、Python Also similar ):
- Boolean value
FALSEIn itself - integer
0( zero ) - Floating point value
0.0( zero ) - An empty string , And strings “0”
- An array that does not include any elements
- Special type NULL( Include variables that have not been assigned )
- Generated from empty tags SimpleXML object
And in the Go Not in language , Values of different types cannot be used == or != Operator to compare , Errors will be reported at compile time , For example, the following code :
b := (false == 0);The following errors will be reported during compilation :
annot convert 0 (type untyped number) to type bool
invalid operation: false == 0 (mismatched types bool and int) Again ,! Operators also cannot act on non Boolean values .
边栏推荐
猜你喜欢

Socket programming (multi process)

After reading five books, I summarized these theories of wealth freedom

Implementation principle and source code analysis of ThreadPoolExecutor thread pool

Image segmentation - improved network structure

typeScript的介绍与变量定义的基本类型

How to start Jupiter notebook in CONDA virtual environment

Ignore overlength parameter violation

Deep learning ----- different methods to implement lenet-5 model

走好数据中台最后一公里,为什么说数据服务API是数据中台的标配?

开源软件、自由软件、Copyleft、CC都是啥,傻傻分不清楚?
随机推荐
How to migrate x86 architecture applications to arm architecture at low cost
Map接口及其子实现类
Commonly used bypass methods for SQL injection -ctf
Cloud computing "half peak"
5-旋转的小菊-旋转画布和定时器
5本财富自由好书的精华
After reading five books, I summarized these theories of wealth freedom
C# 内存法复制图像bitmap
Vulnhub | DC: 4 |【实战】
Imperva- method of finding regular match timeout
Deep learning ----- different methods to implement lenet-5 model
MySQL common skills
Socket socket programming
After easynvr video is enabled, no video file is generated. How to solve this problem?
数据资产为王,解析企业数字化转型与数据资产管理的关系
socket编程(多线程)
Implementation of AVL tree
View the file once a second and send the result of the last line of the file to the syslog server
How to start Jupiter notebook in CONDA virtual environment
Structure and usage of transform