标签:错误信息 创建 运算 slice 信息 data 使用 VID UNC
package main import "fmt" var x, y int var ( // 这种因式分解关键字的写法一般用于声明全局变量 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 = true fmt.Println("Hello, World!",a,b,c) //classic for a:=0;a<2;a++ {fmt.Println(a)} //while d:=0 for(d<2){fmt.Println(d);d++} //forever loop for{d++;if(d==10){fmt.Println("break");break}} var c1 Circle c1.radius = 10.00 fmt.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=110 printBooks(&Book2) //splitArr splitArrar() //range rnage_t() //map map_t() //delete delete_t() //递归 // factorial(5) fmt.Println(factorial(5)) //interface var iphone IPhone iphone.call() var phone Phone phone=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 } //poniter func max(a *int,b *int) int { if ( *a > *b){ return *a }else{ return *b } } //funftion func add(a int,b int) int { c := a + b fmt.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 arrar var 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,3 s=append(s,1,2,3) fmt.Println(s) //copy befor Expansion of consumption s1=make([]int,len(s),cap(s)*2) copy(s1,s) fmt.Println(s1) for e := range s1{ fmt.Println(s1[e]) } } //range func rnage_t() { num := []int{1,2,3,4,5} var sum int=0 //index,value for _,n := range num{ sum += n } fmt.Println(sum) var str string="abcd" for i:=range str{ fmt.Println(str[i]) } } //map func 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]) } } //delete func 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=0 func 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, "" } }
标签:错误信息 创建 运算 slice 信息 data 使用 VID UNC
原文地址:https://www.cnblogs.com/fushou/p/9072598.html