golang經常使用手冊:數據類型、變量和常量

你們好,我又來了,接着上一期咱們搭建了golang的環境變量後,咱們接着看一下編程的基礎常識。html

衆所周知在編程中,咱們涉及到一下經常使用的東西:java

  • 數據類型
  • 變量和常量
  • 運算符
  • 語句
  • 數組、集合
  • 方法
  • 資源引入

一、數據類型golang

在go語言中,咱們常見的數據類型有不少,可是值得注意的是,他們都是小寫!都是小寫!都是小寫!shell

package builtin

// bool is the set of boolean values, true and false.
type bool bool

// true and false are the two untyped boolean values.
const (
	true  = 0 == 0 // Untyped bool.
	false = 0 != 0 // Untyped bool.
)

// uint8 is the set of all unsigned 8-bit integers.
// Range: 0 through 255.
type uint8 uint8

// uint16 is the set of all unsigned 16-bit integers.
// Range: 0 through 65535.
type uint16 uint16

// uint32 is the set of all unsigned 32-bit integers.
// Range: 0 through 4294967295.
type uint32 uint32

// uint64 is the set of all unsigned 64-bit integers.
// Range: 0 through 18446744073709551615.
type uint64 uint64

// int8 is the set of all signed 8-bit integers.
// Range: -128 through 127.
type int8 int8

// int16 is the set of all signed 16-bit integers.
// Range: -32768 through 32767.
type int16 int16

// int32 is the set of all signed 32-bit integers.
// Range: -2147483648 through 2147483647.
type int32 int32

// int64 is the set of all signed 64-bit integers.
// Range: -9223372036854775808 through 9223372036854775807.
type int64 int64

// float32 is the set of all IEEE-754 32-bit floating-point numbers.
type float32 float32

// float64 is the set of all IEEE-754 64-bit floating-point numbers.
type float64 float64

// complex64 is the set of all complex numbers with float32 real and
// imaginary parts.
type complex64 complex64

// complex128 is the set of all complex numbers with float64 real and
// imaginary parts.
type complex128 complex128

// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.
type string string

// int is a signed integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, int32.
type int int

// uint is an unsigned integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, uint32.
type uint uint

// uintptr is an integer type that is large enough to hold the bit pattern of
// any pointer.
type uintptr uintptr

// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8

// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32

// iota is a predeclared identifier representing the untyped integer ordinal
// number of the current const specification in a (usually parenthesized)
// const declaration. It is zero-indexed.
const iota = 0 // Untyped int.

// nil is a predeclared identifier representing the zero value for a
// pointer, channel, func, interface, map, or slice type.
var nil Type // Type must be a pointer, channel, func, interface, map, or slice type

// Type is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type int

// Type1 is here for the purposes of documentation only. It is a stand-in
// for any Go type, but represents the same type for any given function
// invocation.
type Type1 int

// IntegerType is here for the purposes of documentation only. It is a stand-in
// for any integer type: int, uint, int8 etc.
type IntegerType int

// FloatType is here for the purposes of documentation only. It is a stand-in
// for either float type: float32 or float64.
type FloatType float32

// ComplexType is here for the purposes of documentation only. It is a
// stand-in for either complex type: complex64 or complex128.
type ComplexType complex64

複製代碼

在上面的源碼摘錄中,咱們能夠看到go語言完整的數據類型。可是,咱們總結下經常使用的以下:編程

  • int、uint 整形、無符號整形
    • int八、int16(2字節,-32768~32767)、int32(rune)、int64
    • uint八、uint16(2字節,0~65535)、uint3二、uint64
  • string 字符串(原生utf-8編碼支持)
  • bool 布爾型
  • float 浮點型
    • float3二、float64 (4/8字節,精確到小數後7/15位)
  • byte 1字節 uint8的別名
  • nil 空類型
  • complex6四、complex128 複數類型(實數、虛數),8字節、16字節
  • uintptr 足夠大的位模式保存指針的整數類型(uintptr is an integer type that is large enough to hold the bit pattern of any pointer.)

一樣的,還有一些派生數據類型(我我的喜歡稱之爲擴展數據類型或者是包裝數據類型),基本以下:數組

  • 指針類型(Pointer)
  • 數組類型
  • 結構化類型(struct)
  • Channel 類型
  • 函數類型
  • 切片類型
  • 接口類型(interface)
  • map類型

固然這些擴展數據類型咱們將在之後一一闡述。bash

go 1.9版本對於數字類型,無需定義int及float,系統會自動識別。ide

附贈demo小案例(完成變量的申明和使用,以及注意事項)函數

package main

//導包
import "fmt"

//分組申明變量
var (
	personA = "小王"
	personC = "小張"
	ageA    = 18
)

func main() {
    //快捷申明變量 省卻var標識符
	mName := "程先生"
	//var 標明數據是變量
	var mSex = "漢子"
	fmt.Println(mName + ",哎喲我去,你是" + mSex)

	var a = 1.5
	var b = 2
	fmt.Println(a, " ====我是分隔符==== ", b)

	fmt.Println(personA, " ====我是分隔符==== ", personC, " ====我是分隔符==== ", ageA)
}
複製代碼

固然,運行結果以下:ui

程先生,哎喲我去,你是漢子
1.5  ====我是分隔符====  2
小王  ====我是分隔符====  小張  ====我是分隔符====  18
複製代碼

在上面的代碼中,有下面一些操做

  • 必須申明包,且主程序包爲:main
  • 入口函數爲:main,且沒有任何參數
  • var爲變量申明標識符
  • 變量能夠分組申明(導包能夠分組嗎?)
  • 能夠採用快捷申明變量 := ,注意:僅僅能在函數中使用
  • 變量的基本數據類型無需申明,系統會自動推導
    • 固然手動申明變量類型如: var aa string = "餈粑"
  • 全部的變量都是使用過的(未使用的變量會產生編譯異常)
  • java中輸出語句基本數據類型和string能夠經過「+」鏈接,可是go中不容許這種騷操做
  • 變量能夠即時申請
  • 無需「;」結尾,換行既是語句的結束
  • 雖然容許多變量一行賦值,可是不推薦如此作,因此也未舉例

常量

常量是一個簡單值的標識符,在程序運行時,不會被修改的量。 常量中的數據類型只能夠是布爾型、數字型(整數型、浮點型和複數)和字符串型。

常量定義格式: const identifier [type] = value

以下:

  • 顯式類型定義: const b string = "abc"
  • 隱式類型定義: const b = "abc"
  • 多個相同類型的聲明能夠簡寫爲: const c_name1, c_name2 = value1, value2 (固然,我同樣不推薦這樣定義,下降了可讀性)

那麼咱們試一試小demo

//在main方法中執行下面代碼段

	const PI = 3.14
	r := 4
	fmt.Println("圓的面積爲:", PI*r*r)
複製代碼

在咱們運行後代碼報錯以下:

# command-line-arguments
./demo001.go:26:38: constant 3.14 truncated to integer
複製代碼

上面代碼提示PI被截斷爲整數,可是程序卻沒能成功運行,是否是go不支持數據類型自動轉換呢?

咱們這時候考慮一下把常量顯示申明爲float32試一試:

const PI float32 = 3.14
	r := 4
	fmt.Println("圓的面積爲:", PI*r*r)
複製代碼

此次更加悽慘,代碼甚至不能經過編譯,在「PI *r *r」這裏提示類型匹配錯誤!emmmm····

咱們只能選擇把r申明爲float試試,以下:

const PI = 3.14
	r := 4.0
	fmt.Println("圓的面積爲:", PI*r*r)
複製代碼

恭喜,本次編譯經過,且有輸出內容,以下:

圓的面積爲: 50.24
複製代碼

固然以下代碼也能成功運行:

const PI float32 = 3.14
	var r float32 = 4.0
	fmt.Println("圓的面積爲:", PI*r*r)
複製代碼

這說明,go是一個嚴格強調數據類型匹配的語言。

固然,前面咱們看到變量分組申明,那麼這裏咱們常量同樣能夠分組申明:

const (
    OK = 0
    ERROR = 1
    EMPUTY = -1
)
複製代碼

常量申明中的 iota

ota,特殊常量,能夠認爲是一個能夠被編譯器修改的常量。 在每個const關鍵字出現時,被重置爲0,而後再下一個const出現以前,每出現一次iota,其所表明的數字會自動增長1。

咱們來枚舉一些常量:

const (
    a = iota
    b = iota
    c = iota
)

//第一個 iota 等於 0,每當 iota 在新的一行被使用時,它的值都會自動加 1;因此 a=0, b=1, c=2 能夠簡寫爲以下形式:

const (
    a = iota
    b
    c
)
複製代碼

固然咱們也能夠看看別人教程(菜鳥教程-> Go語言常量)所書寫的例子:

package main

import "fmt"

func main() {
    const (
            a = iota   //0
            b          //1
            c          //2
            d = "ha"   //獨立值,iota += 1
            e          //"ha" iota += 1
            f = 100    //iota +=1
            g          //100 iota +=1
            h = iota   //7,恢復計數
            i          //8
    )
    fmt.Println(a,b,c,d,e,f,g,h,i)
}
複製代碼

運行結果以下:

0 1 2 ha ha 100 100 7 8
複製代碼

上面的例子,說實話,有一點繞,同時我也不建議在實際開發中寫出這種騷操做,畢竟這樣寫可能會被隊友打死。

固然iota也能夠加入到移位運算中:

package main

import "fmt"
const (
    //第一個iota默認值是0,左移位1,則不變
    i=1<<iota
    //左移位 0+1,3的二進制是0011,左移位一次就是0110,十進制結果爲6
    j=3<<iota
    //這裏實際代碼是: k = 3 << iota ,按照上面的推算則是左移位1+1,下面的l同理
    k
    l
)

func main() {
    fmt.Println("i=",i)
    fmt.Println("j=",j)
    fmt.Println("k=",k)
    fmt.Println("l=",l)
}
複製代碼

輸出結果:

i= 1
j= 6
k= 12
l= 24
複製代碼

總結

變量的申明:

  • var name [type] = value
  • 方法內 name := value
  • 變量組申明 var (name = value)

常量的申明:

  • const name [type] = value
  • 常量組申明:const (name = value)

其餘:

  • 內置函數
  • 變量常量數據類型自推導
  • 嚴格的數據類型限制
相關文章
相關標籤/搜索