当前位置:网站首页>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  FALSE  In 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 .

原网站

版权声明
本文为[weixin_ fifty-nine million two hundred and eighty-four thousand]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230749172456.html