標籤:錯誤資訊 建立 運算 slice 資訊 data 使用 VID UNC
package mainimport "fmt"var x, y intvar ( // 這種因式分解關鍵字的寫法一般用於聲明全域變數 a int b bool)/* 定義結構體 */type Circle struct {radius float64}type Books struct { title string author string subject string book_id int}func main() {var a = "菜鳥教程"const b string = "runoob.com"var c bool = truefmt.Println("Hello, World!",a,b,c)//classicfor a:=0;a<2;a++ {fmt.Println(a)}//whiled:=0for(d<2){fmt.Println(d);d++}//forever loopfor{d++;if(d==10){fmt.Println("break");break}} var c1 Circlec1.radius = 10.00fmt.Println("Area of Circle(c1) = ", c1.getArea())var Book2 Books /* 聲明 Book2 為 Books 類型 */Book2.title="test.go"Book2.author="fsrong"Book2.subject="trinne"Book2.book_id=110printBooks(&Book2)//splitArrsplitArrar()//rangernage_t()//mapmap_t()//deletedelete_t()//遞迴// factorial(5)fmt.Println(factorial(5))//interfacevar iphone IPhoneiphone.call()var phone Phonephone=new(NokiaPhone)phone.call()phone=new(IPhone)phone.call()//error interface // 正常情況 if result, errorMsg := Divide(100, 10); errorMsg == "" { fmt.Println("100/10 = ", result) } // 當被除數為零的時候會返回錯誤資訊 if _, errorMsg := Divide(100, 0); errorMsg != "" { fmt.Println("errorMsg is: ", errorMsg) } var div DivideError;div.divider=10;div.dividee=100; fmt.Println(div.Error()) var err error=new(DivideError) fmt.Println(err.Error())}//該 method 屬於 Circle 類型對象中的方法func (cc Circle) getArea() float64 { //c.radius 即為 Circle 類型對象中的屬性 return 3.14 * cc.radius * cc.radius}//poniterfunc max(a *int,b *int) int {if ( *a > *b){ return *a}else{return *b}}//funftionfunc add(a int,b int) int {c := a + bfmt.Println(c)return c}//結構體作為函數參數func printBooks(book1 *Books) {fmt.Println(book1.title)fmt.Println(book1.author)fmt.Println(book1.subject)fmt.Println(book1.book_id)}//Go 語言切片(Slice)func splitArrar() {//split arrarvar arr =[]int{1,2,3,4}s:=arr[1:]fmt.Println(s)s=arr[2:len(arr)]fmt.Println(s)s1:=make([]int,3,5) fmt.Println(s1)fmt.Println(cap(arr))//append 1,2,3s=append(s,1,2,3)fmt.Println(s)//copy befor Expansion of consumptions1=make([]int,len(s),cap(s)*2)copy(s1,s)fmt.Println(s1)for e := range s1{fmt.Println(s1[e])}}//rangefunc rnage_t() {num := []int{1,2,3,4,5}var sum int=0//index,valuefor _,n := range num{sum += n}fmt.Println(sum)var str string="abcd"for i:=range str{fmt.Println(str[i])}}//mapfunc map_t() {// var strStrMap map[string]string//如果不初始化 map,那麼就會建立一個 nil map。nil map 不能用來存放索引值對strStrMap := make(map[string]string)strStrMap["a"]="A"strStrMap["b"]="B"strStrMap["c"]="C"strStrMap["d"]="D"fmt.Println(strStrMap)for key:= range strStrMap{fmt.Println(strStrMap[key])}}//deletefunc delete_t() {/* 建立map */ strStrMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"} fmt.Println("原始地圖") /* 列印地圖 */ for country := range strStrMap { fmt.Println(country, "首都是", strStrMap [ country ]) } /*刪除元素*/ delete(strStrMap, "France") fmt.Println("法國條目被刪除") fmt.Println("刪除元素後地圖") for country := range strStrMap { fmt.Println(country, "首都是", strStrMap [ country ]) }}//遞迴,階乘之和var sum uint64=0func factorial(n uint64)(result uint64) { if (n > 0) { result = n * factorial(n-1) sum += result fmt.Println(sum) return result } return 1}//interface 介面中的方法一定要被使用者實現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!")}//error類型是一個介面類型,這是它的定義type error interface { Error() string}// 定義一個 DivideError 結構type DivideError struct { dividee int divider int}// 實現 `error` 介面func (de *DivideError) Error() string { strFormat := ` Cannot proceed, the divider is zero. dividee: %d divider: %d` return fmt.Sprintf(strFormat, de.dividee,de.divider)}// 定義 `int` 類型除法運算的函數func Divide(varDividee int, varDivider int) (result int, errorMsg string) { if varDivider == 0 { dData := DivideError{ dividee: varDividee, divider: varDivider, } errorMsg = dData.Error() return } else { return varDividee / varDivider, "" }}
go語言學習