golang语言部分保留字的举例

时间:2023-03-09 01:44:48
golang语言部分保留字的举例
  golang和c的代码有很大不同的,一不小心就会误用。
1 /* go保留字: */
/*
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var */
/*1 package Name 包的名字 */
//package go_reserved
package main //No1
/*2 import "Name" 导入包
import (
别名 "fmt" 一旦启用别名,就不能再使用原名了.
"..."
)
*/
import ( //No2
f "fmt"
"io"
"os"
"strconv"
)
/*3 func 函数定义(形参)(返回列表类型)
return 函数返回
func func_name()(X .. Xn's type){
...
...
return X .. Xn
}
func (p mytype) funcname(q int) (r,s int) { return 0,0 }
func: 函数保留字
(p mytype): method,这部分称之为receiver而它是可选的.
funcname: 函数名
(q int): 输入参数
(r,s int): 返回值类型列表
{ return 0,0 }: 函数体
*/
func sum(a int, b int)(int){//No3
return a+b
}
/*4 var 变量定义
var a int
var b bool
var c,d int
var (
a int
b bool
c, d = 3, 4
)
c ,d := 3, 4
*/
func func_var(){
f.Println("var测试举例:")
var (//No4
a, b = ,
c int
)
f.Println("I Love Golang")
sum := sum(a,b)
c=sum
f.Println("3+5=",c)
}
/*5 const 变量名: 只能是数字、字符串或布尔值.
const x = 3
const (
a = iota
b = iota
c //默认是c=iota
...
)
*/
func func_const(){
f.Println("const生成枚举值举例:")
const ( //No5
aa = iota
ab
as = "Love"
ac
ad = iota
ae
)
f.Println("const: ", aa, ab, as, ac, ad, ae)
}
/*6 7 if (语句;)条件 {
...
} else {
...
}
*/
func func_if(){
f.Println("条件判断(if...else...)举例:")
const ( //No5
aa = iota
ab
as = "Love"
ac
ad = iota
ae
)
//条件判断表达式
if aa == { //No6
f.Println("aa is:", aa)
}else if (aa+) == {
f.Println("ab is:", ab)
}else{ //No7
f.Println(as)
}
} /*8 for .. ;条件; .. {
...
}
*/
func func_for(){
f.Println("循环判断(for)及数组定义举例:")
//定义数组方法.
var arr[] int;
for i:=; i<; i++{//No7 跳出for循环,i就失效了.
arr[i]=i
}
for i:=; i<; i++{
f.Printf("arr[%d]:%d ",i,arr[i])
}
f.Println("");
a3:=[] int {,,}
for i:=; i<; i++{
f.Printf("a[%d]:%d ",i,a3[i])
}
f.Println("");
a4 := [...] int {,,,,,,}
for i:=; i<; i++{
f.Printf("a[%d]:%d ",i,a4[i])
}
f.Println("")
a := [][] int {[] int{,}, [] int {,}}
b := [][] int {[...] int {,}, [...] int {,}}
c := [][] int {{,},{}}
for i:=; i<; i++{
for j:=; j<; j++{
f.Printf("a[%d][%d]=%d, b[%d][%d]=%d, c[%d][%d]=%d\n",+
i,j,a[i][j],i,j,b[i][j],i,j,c[i][j])
}
}
f.Println("");
}
/*9 switch 语句,变量,常量,... {
10 case X1,...,Xn:
...
11 case Y1,...,Yn: fallthrough //若匹配不到,则自动向下尝试.
case Z1:
f() //当匹配到Z1时,因为有fallthrough所以会调用f().
...
12 default:
...
}
*/
func func_switch(key int){
f.Println("switch { case: .. case: .. default: ... }接口测试举例:")
const ( //No5
aa = iota
ab
as
ac
ad
ae
)
switch key{//No9
case aa: //No10
f.Println("key is", aa)
case ab, as: //ab, as is display ab
f.Println("key is", ab)
case ac: //ac is lost
case ad:
f.Println("key is", ad)
case ae: fallthrough //No11 ae is not lost
default: //No12
f.Println("Default key is", key)
}
}
/*13 type 别名
type mytype int
*/
func func_type(){
f.Println("type测试举例:")
type mytype int //No13
var a mytype
a=
f.Println(a)
}
/*14 map[<from type>]<to type>
15 range
在 Go 中有 map 类型.
map 可以认为是一个用字符串做索引的数组(在其最简单的形式下)
*/
func func_map(){
monthdays := map[string]int{
"Jan":,"Feb":,"Mar":,"Apr":,
"May":,"Jun":,"Jul":,"Aug":,
"Sep":,"Oct":,"Nov":,"Dec":, //最后必须有逗号,
}
md := make(map[string]int) //No14 声明map的方法.
md = monthdays; //这种方式是指向同一个内存,若操作均发生变化.
f.Println(monthdays)
f.Println(md)
value,ok := monthdays["Jun"]
f.Println("md's length=",len(md), "monthdays' length=", len(monthdays), value, ok)
monthdays["Jun"] = , false //删除该元素 方法1.
v,o := monthdays["Jun"]
f.Println(monthdays)
f.Println(md)
f.Println("md's length=",len(md), "monthdays' length=", len(monthdays), v, o)
mk := make(map[string]int) //声明map的方法.
for key,ve := range monthdays{ //No15
mk[key]=ve //这种方式赋值,不会使用monthdays的空间.
}
f.Println(mk)
delete(mk, "Jan") //删除该元素 方法2.
f.Println("mk is:", mk)
f.Println("monthdays is", monthdays)
}
/*16 goto
goto标签,函数内部跳转.
*/
func func_goto(){
i :=
next:
if i<{
f.Printf("%d ", i)
i=i+
goto next //No16
}
f.Printf("\ni=%d>=10, Func_goto is stoped!\n", i)
}
/*17 defer 延后执行方法
18 return 返回值列表
func func_name(){
file.Open("filename")
defer file.Close() //在func_name()函数退出前执行close
...
}
*/
func readfile() string {
fi,err := os.Open("json.go")
if err != nil{
panic(err)
}
defer fi.Close() //No17
chunks := make([]byte,,)
buf := make([]byte,)
for{
n,err := fi.Read(buf)
if err != nil && err != io.EOF{
panic(err)
}
if ==n { break }
chunks=append(chunks, buf[:n]...)
f.Println(string(buf[:n]))
}
return string(chunks) //No18
}
/*19 struct 结构体
type 别名 struct{
var1 vartype
var2 vartype
...
}
*20 continue 继续下一个
*21 break 跳出本次循环
*/
func func_struct(){
type student struct{ //No19
id int
score int
name string
}
var s[] student
for i:=; i<; i=i+{
s[i].id=i+
s[i].score=i+
s[i].name="Name"+strconv.Itoa(i)
}
for i:=; i<; i++{
if i%== { continue }//No20
if i%== { break }//No21
f.Println(s[i])
}
for i,value:=range s{
f.Println(i, value)
}
f.Println(s)
}
/*22 interface
方法集合
*/
type S struct {
i int
}
func (p *S) Get() int{
return p.i
}
func (p *S) Put(v int){
p.i=v
}
type I interface{
Get() int
Put(int)
}
func func_interface(p I){
p.Put()
f.Println("Interface is:",p.Get())
}
/*23 go
*/
func myprint(s string){
f.Printf("%v is Ready!\n", s)
}
func func_go(){
go myprint("I")
go myprint("Love")
go myprint("Golang")
f.Println("func_go is Waiting")
}