golang语言部分保留字的举例

  golang和c的代码有很大不同的,一不小心就会误用。
1
/* go保留字: */ 2 /* 3 break default func interface select 4 case defer go map struct 5 chan else goto package switch 6 const fallthrough if range type 7 continue for import return var 8 9 */ 10 /*1 package Name 包的名字 */ 11 //package go_reserved 12 package main //No1 13 /*2 import "Name" 导入包 14 import ( 15 别名 "fmt" 一旦启用别名,就不能再使用原名了. 16 "..." 17 ) 18 */ 19 import ( //No2 20 f "fmt" 21 "io" 22 "os" 23 "strconv" 24 ) 25 /*3 func 函数定义(形参)(返回列表类型) 26 return 函数返回 27 func func_name()(X .. Xn's type){ 28 ... 29 ... 30 return X .. Xn 31 } 32 func (p mytype) funcname(q int) (r,s int) { return 0,0 } 33 func: 函数保留字 34 (p mytype): method,这部分称之为receiver而它是可选的. 35 funcname: 函数名 36 (q int): 输入参数 37 (r,s int): 返回值类型列表 38 { return 0,0 }: 函数体 39 */ 40 func sum(a int, b int)(int){//No3 41 return a+b 42 } 43 /*4 var 变量定义 44 var a int 45 var b bool 46 var c,d int 47 var ( 48 a int 49 b bool 50 c, d = 3, 4 51 ) 52 c ,d := 3, 4 53 */ 54 func func_var(){ 55 f.Println("var测试举例:") 56 var (//No4 57 a, b = 3, 5 58 c int 59 ) 60 f.Println("I Love Golang") 61 sum := sum(a,b) 62 c=sum 63 f.Println("3+5=",c) 64 } 65 /*5 const 变量名: 只能是数字、字符串或布尔值. 66 const x = 3 67 const ( 68 a = iota 69 b = iota 70 c //默认是c=iota 71 ... 72 ) 73 */ 74 func func_const(){ 75 f.Println("const生成枚举值举例:") 76 const ( //No5 77 aa = iota 78 ab 79 as = "Love" 80 ac 81 ad = iota 82 ae 83 ) 84 f.Println("const: ", aa, ab, as, ac, ad, ae) 85 } 86 /*6 7 if (语句;)条件 { 87 ... 88 } else { 89 ... 90 } 91 */ 92 func func_if(){ 93 f.Println("条件判断(if...else...)举例:") 94 const ( //No5 95 aa = iota 96 ab 97 as = "Love" 98 ac 99 ad = iota 100 ae 101 ) 102 //条件判断表达式 103 if aa == 1 { //No6 104 f.Println("aa is:", aa) 105 }else if (aa+1) == 1 { 106 f.Println("ab is:", ab) 107 }else{ //No7 108 f.Println(as) 109 } 110 } 111 112 /*8 for .. ;条件; .. { 113 ... 114 } 115 */ 116 func func_for(){ 117 f.Println("循环判断(for)及数组定义举例:") 118 //定义数组方法. 119 var arr[10] int; 120 for i:=0; i<10; i++{//No7 跳出for循环,i就失效了. 121 arr[i]=i 122 } 123 for i:=0; i<10; i++{ 124 f.Printf("arr[%d]:%d ",i,arr[i]) 125 } 126 f.Println(""); 127 a3:=[3] int {1,2,3} 128 for i:=0; i<3; i++{ 129 f.Printf("a[%d]:%d ",i,a3[i]) 130 } 131 f.Println(""); 132 a4 := [...] int {1,2,3,4,5,6,7} 133 for i:=0; i<7; i++{ 134 f.Printf("a[%d]:%d ",i,a4[i]) 135 } 136 f.Println("") 137 a := [2][2] int {[2] int{1,2}, [2] int {3,4}} 138 b := [2][2] int {[...] int {5,6}, [...] int {7,8}} 139 c := [2][2] int {{9,10},{11}} 140 for i:=0; i<2; i++{ 141 for j:=0; j<2; j++{ 142 f.Printf("a[%d][%d]=%d, b[%d][%d]=%d, c[%d][%d]=%d\n",+ 143 i,j,a[i][j],i,j,b[i][j],i,j,c[i][j]) 144 } 145 } 146 f.Println(""); 147 } 148 /*9 switch 语句,变量,常量,... { 149 10 case X1,...,Xn: 150 ... 151 11 case Y1,...,Yn: fallthrough //若匹配不到,则自动向下尝试. 152 case Z1: 153 f() //当匹配到Z1时,因为有fallthrough所以会调用f(). 154 ... 155 12 default: 156 ... 157 } 158 */ 159 func func_switch(key int){ 160 f.Println("switch { case: .. case: .. default: ... }接口测试举例:") 161 const ( //No5 162 aa = iota 163 ab 164 as 165 ac 166 ad 167 ae 168 ) 169 switch key{//No9 170 case aa: //No10 171 f.Println("key is", aa) 172 case ab, as: //ab, as is display ab 173 f.Println("key is", ab) 174 case ac: //ac is lost 175 case ad: 176 f.Println("key is", ad) 177 case ae: fallthrough //No11 ae is not lost 178 default: //No12 179 f.Println("Default key is", key) 180 } 181 } 182 /*13 type 别名 183 type mytype int 184 */ 185 func func_type(){ 186 f.Println("type测试举例:") 187 type mytype int //No13 188 var a mytype 189 a=10 190 f.Println(a) 191 } 192 /*14 map[<from type>]<to type> 193 15 range 194 在 Go 中有 map 类型. 195 map 可以认为是一个用字符串做索引的数组(在其最简单的形式下) 196 */ 197 func func_map(){ 198 monthdays := map[string]int{ 199 "Jan":31,"Feb":28,"Mar":31,"Apr":30, 200 "May":31,"Jun":30,"Jul":31,"Aug":31, 201 "Sep":30,"Oct":31,"Nov":30,"Dec":31, //最后必须有逗号, 202 } 203 md := make(map[string]int) //No14 声明map的方法. 204 md = monthdays; //这种方式是指向同一个内存,若操作均发生变化. 205 f.Println(monthdays) 206 f.Println(md) 207 value,ok := monthdays["Jun"] 208 f.Println("md's length=",len(md), "monthdays' length=", len(monthdays), value, ok) 209 monthdays["Jun"] = 0, false //删除该元素 方法1. 210 v,o := monthdays["Jun"] 211 f.Println(monthdays) 212 f.Println(md) 213 f.Println("md's length=",len(md), "monthdays' length=", len(monthdays), v, o) 214 mk := make(map[string]int) //声明map的方法. 215 for key,ve := range monthdays{ //No15 216 mk[key]=ve //这种方式赋值,不会使用monthdays的空间. 217 } 218 f.Println(mk) 219 delete(mk, "Jan") //删除该元素 方法2. 220 f.Println("mk is:", mk) 221 f.Println("monthdays is", monthdays) 222 } 223 /*16 goto 224 goto标签,函数内部跳转. 225 */ 226 func func_goto(){ 227 i := 0 228 next: 229 if i<10{ 230 f.Printf("%d ", i) 231 i=i+1 232 goto next //No16 233 } 234 f.Printf("\ni=%d>=10, Func_goto is stoped!\n", i) 235 } 236 /*17 defer 延后执行方法 237 18 return 返回值列表 238 func func_name(){ 239 file.Open("filename") 240 defer file.Close() //在func_name()函数退出前执行close 241 ... 242 } 243 */ 244 func readfile() string { 245 fi,err := os.Open("json.go") 246 if err != nil{ 247 panic(err) 248 } 249 defer fi.Close() //No17 250 chunks := make([]byte,1024,1024) 251 buf := make([]byte,1024) 252 for{ 253 n,err := fi.Read(buf) 254 if err != nil && err != io.EOF{ 255 panic(err) 256 } 257 if 0==n { break } 258 chunks=append(chunks, buf[:n]...) 259 f.Println(string(buf[:n])) 260 } 261 return string(chunks) //No18 262 } 263 /*19 struct 结构体 264 type 别名 struct{ 265 var1 vartype 266 var2 vartype 267 ... 268 } 269 *20 continue 继续下一个 270 *21 break 跳出本次循环 271 */ 272 func func_struct(){ 273 type student struct{ //No19 274 id int 275 score int 276 name string 277 } 278 var s[10] student 279 for i:=0; i<10; i=i+1{ 280 s[i].id=i+10 281 s[i].score=i+10 282 s[i].name="Name"+strconv.Itoa(i) 283 } 284 for i:=0; i<10; i++{ 285 if i%2==0 { continue }//No20 286 if i%5==0 { break }//No21 287 f.Println(s[i]) 288 } 289 for i,value:=range s{ 290 f.Println(i, value) 291 } 292 f.Println(s) 293 } 294 /*22 interface 295 方法集合 296 */ 297 type S struct { 298 i int 299 } 300 func (p *S) Get() int{ 301 return p.i 302 } 303 func (p *S) Put(v int){ 304 p.i=v 305 } 306 type I interface{ 307 Get() int 308 Put(int) 309 } 310 func func_interface(p I){ 311 p.Put(15) 312 f.Println("Interface is:",p.Get()) 313 } 314 /*23 go 315 */ 316 func myprint(s string){ 317 f.Printf("%v is Ready!\n", s) 318 } 319 func func_go(){ 320 go myprint("I") 321 go myprint("Love") 322 go myprint("Golang") 323 f.Println("func_go is Waiting") 324 }

 

本文来自:博客园

感谢作者:neujie

查看原文:golang语言部分保留字的举例

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。