繼golang第一天後,今天學習下golang的變數、常量、數據類型和控制流語句。 做過其他編程語言(比如JavaScript,java,python)項目的話,其實很好理解變數、常量、數據類型和控制流。 變數也被稱為“變數”,是反映事物運動變化狀態的量,比如匯率、房貸利率、貸款利率。 常量也被稱“ ...
繼golang第一天後,今天學習下golang的變數、常量、數據類型和控制流語句。
做過其他編程語言(比如JavaScript,java,python)項目的話,其實很好理解變數、常量、數據類型和控制流。
變數也被稱為“變數”,是反映事物運動變化狀態的量,比如匯率、房貸利率、貸款利率。
常量也被稱“常數”,是反映事物相對靜止狀態的量,一旦定義,後續不能更改,比如圓周率PI。
Golang是不同於JavaScript和python,但它和java一樣,是一種靜態類型的編程語言,就是說定義變數或常量前需要聲明其類型
1. 變數
1.1 聲明一個變數
變數在使用前需要聲明,例如
package main
import "fmt"
func main() {
var age int //age預設是0
fmt.Printf("我的年齡是:%d 歲\n", age)
age = 99 //給age賦值
fmt.Printf("我的年齡是:%d 歲", age)
}
輸出如圖
1.2 自動推導變數類型
也可以用另一種寫法,根據值推導變數的類型
package main
import "fmt"
func main() {
var age = 99
fmt.Printf("我的年齡是:%d 歲\n", age)
age = 100 //
}
輸出結果
1.3 速記法定義變數
還有一種更簡潔的寫法
import "fmt"
func main() {
//fmt.Printf("hello, world\n")
//var name string
age := 99
fmt.Printf("我的年齡是:%d 歲\n", age)
age = 100
}
1.4 定義多個變數
真正開發時我們需要定義多個變數,比如一個用戶有姓名,年齡,性別,地址,城市等多個欄位
我是用1.1的寫法定義變數的
package main
import "fmt"
func main() {
//fmt.Printf("hello, world\n")
//var name string
var (
name string = "董廣明"
age int = 99
city string = "金陵城"
)
fmt.Printf("我叫:%s ,年齡:(%d),所在城市:%s", name, age, city)
}
輸出結果
1.5 函數式變數
JavaScript中這種定義很常見,一個變數可以是任何類型,包括函數,golang也支持,真是融合了好幾種語言的特性,java並不支持
package main
import "fmt"
func main() {
//變數也可以是函數定義
print := func() {
var (
name string = "董廣明,dgm"
age int = 99
city string = "金陵其實就是南京"
)
fmt.Printf("我叫:%s ,年齡:(%d),所在城市:%s", name, age, city)
}
print()
}
輸出結果
2 常量
常量定義和變數類似,只是多了個關鍵字const
package main
import "fmt"
const (
Pi = 3.141592653
Loading = false
Name = "dongguangming"
Age = 99
)
func main() {
const City = "金陵城"
fmt.Println(City)
fmt.Println(Pi)
fmt.Println(Loading)
fmt.Println(Name)
fmt.Println(Age)
//const CompanyName := "某公司" //不支持, 編譯不過去
}
輸出結果
常量小結:Constants can only be character, string, boolean, or numeric values and cannot be declared using the :=
syntax. An untyped constant takes the type needed by its context.
3. 數據類型
分兩種:基本數據類型和derived派生數據類型,在此只先介紹基本的數據類型,後一種以後單獨介紹
3.1 基本數據類型
基本數據類型又有以下幾種類型:布爾bool,字元串string,數值型number,複數型數據類型
3.1.1 布爾bool
bool類型表示布爾邏輯,它的值要麼為true,要麼為false
package main
import "fmt"
func main() {
have := true
nohave := false
fmt.Println("have:", have, "nohave:", nohave)
result_and := have && nohave
fmt.Println("result_and:", result_and)
result_or := have || nohave
fmt.Println("result_or:", result_or)
}
以上代碼中,定義了 變數have並賦值為true和變數nohave並賦值為false
變數result_and被賦值為false,因為邏輯操作符&&表示兩邊的值都為true的情況下才返回ture,例子中只有have為true,故運算結果為false。
變數result_or被賦值為true,因為邏輯操作符||表示兩邊的值只要有一個為true的情況下就返回ture了,例子中剛好有have為true,故運算結果為true。
執行以上輸出以下結果
理論分析是正確的
3.1.2 字元串String類型
字元串是位元組的集合,存儲了字元序列。
package main
import (
"fmt"
)
func main() {
first := "董"
last := "廣明"
name := first + last
fmt.Println("我的名字叫: ",name)
}
輸出結果:
3.1.3 數值型
數值型細分有以下幾種
Numeric types:
uint either 32 or 64 bits,represents 32 or 64 bit unsigned integers depending on the underlying platform,32 bits in 32 bit systems and 64 bits in 64 bit systems( 0 to 4294967295 in 32 bit systems and 0 to 18446744073709551615 in 64 bit systems ).
int either 32 or 64 bits,represents 32 or 64 bit integers depending on the underlying platform. You should generally be using int to represent integers unless there is a need to use a specific sized integer,32 bits in 32 bit systems and 64 bit in 64 bit systems(-2147483648 to 2147483647 in 32 bit systems and -9223372036854775808 to 9223372036854775807 in 64 bit systems)
uintptr an unsigned integer large enough to store the uninterpreted bits of
a pointer value
uint8 the set of all unsigned 8-bit integers (0 to 255)
uint16 the set of all unsigned 16-bit integers (0 to 65535)
uint32 the set of all unsigned 32-bit integers (0 to 4294967295)
uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)
int8 the set of all signed 8-bit integers (-128 to 127)
int16 the set of all signed 16-bit integers (-32768 to 32767)
int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64 the set of all signed 64-bit integers
(-9223372036854775808 to 9223372036854775807)
float32 the set of all IEEE-754 32-bit floating-point numbers
float64 the set of all IEEE-754 64-bit floating-point numbers
complex64 the set of all complex numbers with float32 real and imaginary parts
complex128 the set of all complex numbers with float64 real and imaginary parts
byte alias for uint8
rune alias for int32 (represents a Unicode code point)
整數式
package main
import (
"fmt"
"unsafe"
)
func main() {
var first int = 88
second := 99
fmt.Println("第一個值=", first, ",第二個值=", second)
fmt.Printf("first的類型是:%T, first的大小 %d", first, unsafe.Sizeof(first)) //type and size of first
fmt.Printf("\nsecond的類型是: %T, second的大小 %d", second, unsafe.Sizeof(second)) //type and size of second
}
浮點數
package main
import (
"fmt"
)
func main() {
first, second := 6.66, 9.99
fmt.Printf("first的類型是 %T second的類型是%T\n", first, second)
sum := first + second
diff := first - second
fmt.Println("相加和是", sum, "相減差是", diff)
}
複數計算
package main
import (
"fmt"
)
func main() {
first := complex(5, 7)
second := 8 + 27i
sum := first + second
fmt.Println("複數相加和:", sum)
diff := first - second
fmt.Println("複數相減差:", diff)
product := first * second
fmt.Println("複數相乘:", product)
}
由於數值型可能會參數數學計算,但golang本身沒有自動轉換功能,故需要手工顯示轉換數據類型
package main
import (
"fmt"
)
func main() {
first := 66 //int
second := 77.7 //float64
//sum := first + second //int + float64 not allowed,編譯不通過,故需要下麵顯示轉換,golang沒有自動轉換功能
sum := first + int(second) //second is converted to int
fmt.Println(sum)
}
但是有例外情況
package main
import (
"fmt"
)
func main() {
first := 10
var second float64 = float64(first) //this statement will not work without explicit conversion
fmt.Println("second:", second)
}
輸出結果
沒有像其他語言轉換後有標誌10.0f
4. 流程語句
分三種:if條件/if else, loop迴圈,switch
4.1 if, if else
4.1.1 if語句
用於指定是否應執行相應的代碼塊,語法結構:
if(condition) {
// 執行condition為true時的代碼邏輯
}
例如
package main
import "fmt"
func main() {
var country = "中國"
var age = 18
if(country == "中國" && age>=18) {
fmt.Printf("你已經是成年人了,該掙錢了\n")
}
}
輸出結果
Note that, You can omit the parentheses () from an if statement in Golang, but the curly braces {} are mandatory -
註意,在golang的世界里你可以不使用if後面的插入語(),但是後面的花括弧是必須的,示例
if country == "中國" && age>=18 {
fmt.Printf("你已經是成年人了,該掙錢了\n")
}
4.1.2 If-Else語句
一個if語句能配合else語句塊使用,當if里的條件為false,else語句塊的邏輯代碼會被執行,代碼結構
if condition {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
示例代碼
package main
import "fmt"
func main() {
var age = 18
if age >= 18 {
fmt.Println("很好,你已經是個成年人了,不是小孩子了!!!")
} else {
fmt.Println("抱歉,你還未成年!")
}
}
輸出結果
4.1.3 If-Else-If鏈
if語句能夠有多個else語句塊,例如
package main
import "fmt"
func main() {
var age = 12
if age>=6 && age < 11 {
fmt.Println("你可能在上小學");
} else if age >= 12 && age < 15 {
fmt.Println("你可能在上初中");
} else if age >= 15 && age < 18 {
fmt.Println("你可能在上高中")
} else {
fmt.Println("上大學了")
}
}
4.1.4 if短語句
if語句允許在條件表達式之前包含一個簡短的聲明語句
package main
import "fmt"
func main() {
if first := 10; first%2 == 0 {
fmt.Printf("%d 是偶數\n", first)
}
if second := 15; second%2 == 0 {
fmt.Printf("%d 是偶數\n", second)
} else {
fmt.Printf("%d 是奇數\n", second)
}
}
特別留意: The variable declared in the short statement is only available inside the if block and it’s else or else-if branches ,
If you’re using a short statement, then you can’t use parentheses. 如下代碼會報錯