結構體
和c一樣
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
// 創建一個新的結構體
fmt.Println(Books{"Go 語言", "www.runoob.com", "Go 語言教程", 6495407})
// 也可以使用 key => value 格式
fmt.Println(Books{title: "Go 語言", author: "www.runoob.com", subject: "Go 語言教程", book_id: 6495407})
// 忽略的字段爲 0 或 空
fmt.Println(Books{title: "Go 語言", author: "www.runoob.com"})
}
{Go 語言 www.runoob.com Go 語言教程 6495407}
{Go 語言 www.runoob.com Go 語言教程 6495407}
{Go 語言 www.runoob.com 0}
結構體指針,函數傳參
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* Declare Book1 of type Book */
var Book2 Books /* Declare Book2 of type Book */
/* book 1 描述 */
Book1.title = "Go 語言"
Book1.author = "www.runoob.com"
Book1.subject = "Go 語言教程"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教程"
Book2.author = "www.runoob.com"
Book2.subject = "Python 語言教程"
Book2.book_id = 6495700
/* 打印 Book1 信息 */
printBook(&Book1)
/* 打印 Book2 信息 */
printBook(&Book2)
}
func printBook( book *Books ) {
fmt.Printf( "Book title : %s\n", book.title)
fmt.Printf( "Book author : %s\n", book.author)
fmt.Printf( "Book subject : %s\n", book.subject)
fmt.Printf( "Book book_id : %d\n", book.book_id)
}
Book title : Go 語言
Book author : www.runoob.com
Book subject : Go 語言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.runoob.com
Book subject : Python 語言教程
Book book_id : 6495700
切片
和py一樣。
package main
import "fmt"
func main() {
var numbers []int
printSlice(numbers)
/* 允許追加空切片 */
numbers = append(numbers, 0)
printSlice(numbers)
/* 向切片添加一個元素 */
numbers = append(numbers, 1)
printSlice(numbers)
/* 同時添加多個元素 */
numbers = append(numbers, 2,3,4)
printSlice(numbers)
/* 創建切片 numbers1 是之前切片的兩倍容量*/
numbers1 := make([]int, len(numbers), (cap(numbers))*2)
/* 拷貝 numbers 的內容到 numbers1 */
copy(numbers1,numbers)
printSlice(numbers1)
}
func printSlice(x []int){
fmt.Printf("len=%d cap=%d slice=%v\n",len(x),cap(x),x)
}
/*
len=0 cap=0 slice=[]
len=1 cap=1 slice=[0]
len=2 cap=2 slice=[0 1]
len=5 cap=6 slice=[0 1 2 3 4]
len=5 cap=12 slice=[0 1 2 3 4]
*/
range
range 關鍵字用於 for 循環中迭代數組(array)、切片(slice)、通道(channel)或集合(map)的元素。在數組和切片中它返回元素的索引和索引對應的值,在集合中返回 key-value 對。
package main
import "fmt"
func main() {
//這是我們使用range去求一個slice的和。使用數組跟這個很類似
nums := []int{2, 3, 4}
sum := 0
for _, num := range nums {
sum += num
}
fmt.Println("sum:", sum)
//在數組上使用range將傳入index和值兩個變量。上面那個例子我們不需要使用該元素的序號,所以我們使用空白符"_"省略了。有時侯我們確實需要知道它的索引。
for i, num := range nums {
if num == 3 {
fmt.Println("index:", i)
}
}
//range也可以用在map的鍵值對上。
kvs := map[string]string{"a": "apple", "b": "banana"}
for k, v := range kvs {
fmt.Printf("%s -> %s\n", k, v)
}
//range也可以用來枚舉Unicode字符串。第一個參數是字符的索引,第二個是字符(Unicode的值)本身。
for i, c := range "go" {
fmt.Println(i, c)
}
}
sum: 9
index: 1
a -> apple
b -> banana
0 103
1 111
map
可以使用內建函數 make 也可以使用 map 關鍵字來定義 Map:
/* 聲明變量,默認 map 是 nil */ var map_variable map[key_data_type]value_data_type /* 使用 make 函數 */ map_variable := make(map[key_data_type]value_data_type)
如果不初始化 map,那麼就會創建一個 nil map。nil map 不能用來存放鍵值對
package main
import "fmt"
func main() {
var countryCapitalMap map[string]string /*創建集合 */
countryCapitalMap = make(map[string]string)
/* map插入key - value對,各個國家對應的首都 */
countryCapitalMap [ "France" ] = "巴黎"
countryCapitalMap [ "Italy" ] = "羅馬"
countryCapitalMap [ "Japan" ] = "東京"
countryCapitalMap [ "India " ] = "新德里"
/*使用鍵輸出地圖值 */
for country := range countryCapitalMap {
fmt.Println(country, "首都是", countryCapitalMap [country])
}
/*查看元素在集合中是否存在 */
capital, ok := countryCapitalMap [ "American" ] /*如果確定是真實的,則存在,否則不存在 */
/*fmt.Println(capital) */
/*fmt.Println(ok) */
if (ok) {
fmt.Println("American 的首都是", capital)
} else {
fmt.Println("American 的首都不存在")
}
}
France 首都是 巴黎
Italy 首都是 羅馬
Japan 首都是 東京
India 首都是 新德里
American 的首都不存在
注:刪除:delete(countryCapitalMap, "France")
語言類型轉換
類型轉換用於將一種數據類型的變量轉換爲另外一種類型的變量。Go 語言類型轉換基本格式如下:
type_name(expression)
type_name 爲類型,expression 爲表達式。
如float32(5)等。
接口實例
package main
import (
"fmt"
)
type Phone interface {
call()
}
type NokiaPhone struct {
}
func (nokiaPhone NokiaPhone) call() {
fmt.Println("I am Nokia, I can call you!")
}
type IPhone struct {
}
func (iPhone IPhone) call() {
fmt.Println("I am iPhone, I can call you!")
}
func main() {
var phone Phone
phone = new(NokiaPhone)
phone.call()
phone = new(IPhone)
phone.call()
}
在上面的例子中,我們定義了一個接口Phone,接口裏面有一個方法call()。然後我們在main函數裏面定義了一個Phone類型變量,並分別爲之賦值爲NokiaPhone和IPhone。然後調用call()方法,輸出結果如下:
I am Nokia, I can call you! I am iPhone, I can call you!