你們好,我又來了,接着上一期咱們搭建了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語言完整的數據類型。可是,咱們總結下經常使用的以下:編程
一樣的,還有一些派生數據類型(我我的喜歡稱之爲擴展數據類型或者是包裝數據類型),基本以下:數組
固然這些擴展數據類型咱們將在之後一一闡述。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
複製代碼
在上面的代碼中,有下面一些操做
常量
常量是一個簡單值的標識符,在程序運行時,不會被修改的量。 常量中的數據類型只能夠是布爾型、數字型(整數型、浮點型和複數)和字符串型。
常量定義格式: const identifier [type] = value
以下:
那麼咱們試一試小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
)
複製代碼
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
複製代碼
總結
變量的申明:
常量的申明:
其餘: