标签:例子 根据 func 任务 引用 hand 外部 def parameter
map&函数var a map[key的类型]value的类型
func defineMap() {
var user map[string]int = make(map[string]int)
user["abc"] = 38
fmt.Printf("user:%v\n",user)
a := make(map[string]int)
a["jack"] = 1001
a["handsome"] = 1002
a["shark"] = 1003
//根据key获取value
fmt.Printf("a[jack]=%v\n",a["jack"])
}
# 用户角色判定
var whiteUser map[int]bool = map[int]bool {
34123: true,
3456334: true,
1:true,
}
func isWhiteUser(UserId int) bool {
_,ok := whiteUser[UserId]
return ok
}
func main() {
UserId := 1001
if isWhiteUser(UserId) {
fmt.Printf("is white user %v\n",UserId)
}else {
fmt.Printf("is Normal user %v\n",UserId)
}
}
func loopMap() {
var m map[string]int
m = map[string]int{
"user01":1001,
"user02":1002,
"user03":1003,
}
for key,val := range m {
fmt.Printf("key:%s,value:%v\n",key,val)
}
}
func delMapkey() {
var m map[string]int
m = map[string]int{
"user01":1001,
"user02":1002,
"user03":1003,
}
delete(m,"user01")
fmt.Printf("%#v\n",m)
fmt.Println(len(m))
}
func yinyongMap() {
a := map[string]int {
"jack":1000,
"rose":1001,
}
a["mike"] = 1003
a["jack"] = 999
fmt.Printf("origin map:%v,address:%p\n",a,&a)
b := a
b["mike"] = 1004
fmt.Printf("after map:a=%v,address_a=%p\nmap:b=%v,address_b=%p\n",
a,&a,b,&b)
}
func youxuMap() {
var m map[string]int
m = map[string]int{
"alice":1001,
"zhansan":1002,
"uloce":1003,
}
var keys[]string
for key,_ :=range m{
keys = append(keys,key)
}
// 按字母有序排序
sort.Strings(keys)
for _,key := range keys {
fmt.Printf("key:%s,value:%v\n",key,m[key])
}
}
func mapSlice() {
var s []map[string]int
s = make([]map[string]int,5)
for k,v := range s {
fmt.Printf("index:%d val: %v\n",k,v)
}
//要对于slice的index对应的map要做初始化操作,否则会panic
s[0]=make(map[string]int,16)
s[0]["abc"] = 100
for k,v := range s {
fmt.Printf("index:%d val: %v\n",k,v)
}
}
func functionName([parametername type]) [return type] {
functionBody
}
func functionname() {
functionbody
}
// 多返回值
func Calc(a,b int) (int,int) {
return a+b,a-b
}
func Calc2(a,b int) (sum,sub int) {
sum = a + b
sub = a - b
// 默认会反回所有的返回值
return
}
func main() {
sum,sub := Calc(10,20)
fmt.Println(sum,sub)
}
func Add(a ...int) int {
//打印可变参数,可变参数其实是个切片
fmt.Printf("func args count %d\n",len(a))
var sum int
for index,arg := range a {
fmt.Printf("arg[%d]=%v\n",index,arg)
sum += arg
}
return sum
}
func main() {
sum := Add(1,2,3)
fmt.Println(sum)
}
func TestDefer() {
defer fmt.Println("hello world")
defer fmt.Println("nihao!")
fmt.Println("dabaojian")
file,err := os.Open("./a.txt")
if err != nil {
fmt.Println("打开文件失败",err)
return
}
//defer 实战
var buf[4096]byte
n,errs := file.Read(buf[:])
if errs != nil {
fmt.Println("打开文件失败",err)
return
}
// 关闭文件句柄
defer file.Close()
fmt.Printf("read %d byte succ,content: %s\n",n,string(buf[:]))
return
}
定义: 没有名字的函数
func TestDefer() {
defer fmt.Println("hello world")
defer fmt.Println("nihao!")
fmt.Println("dabaojian")
file,err := os.Open("./a.txt")
defer func(){
if file !=nil {
file.Close()
}
}()
var buf[4096]byte
n,errs := file.Read(buf[:])
if errs != nil {
fmt.Println("打开文件失败",err)
return
}
fmt.Printf("read %d byte succ,content: %s\n",n,string(buf[:]))
return
}
func TestDefer() {
defer fmt.Println("hello world")
defer fmt.Println("nihao!")
fmt.Println("dabaojian")
file,err := os.Open("./a.txt")
defer func(f *os.File) {
if f != nil {
f.Close()
}
}(file)
var buf[4096]byte
n,errs := file.Read(buf[:])
if errs != nil {
fmt.Println("打开文件失败",err)
return
}
fmt.Printf("read %d byte succ,content: %s\n",n,string(buf[:]))
return
}
func calc(op func(args ...int)int,op_args ...int) int{
result :=op(op_args...)
fmt.Printf("result %d\n",result)
return result
}
func main() {
calc(func(args ...int)int{
var sum int
for i:=0;i<len(args);i++{
sum += args[i]
}
return sum
},1,2,3,4,5)
calc(func(args ...int)int{
var sub int
for i:=0;i<len(args);i++{
sub -= args[i]
}
return sub
},1,2,3,4,5)
}
func add(args ...int)int{
var sum int
for i:=0;i<len(args);i++{
// 再一次证明可变参数是切片
sum += args[i]
}
return sum
}
func main() {
fmt.Printf("输出:%d\n",add(1,2,3,4,5))
}
func Adder() func(int) int {
//闭包,定义外部的x,默认是0
var x int
return func(d int) int {
// 引用外部环境的x,并对此做操作,从而改变外部的x的值
x += d
return x
}
}
func main() {
var f = Adder()
fmt.Println(f(1))
fmt.Println(f(20))
fmt.Println(f(300))
fmt.Println("========分割线======")
// 接下来就验证了闭包重新调用会将外部引用的因素还原
var f2 = Adder()
fmt.Println(f2(1))
fmt.Println(f2(20))
fmt.Println(f2(300))
}
// base 作为全局变量
func add(base int) func(int) int {
return func(i int) int {
base += i
return base
}
}
func main() {
tmp1 := add(0)
fmt.Println(tmp1(1),tmp1(2))
tmp2 := add(100)
fmt.Println(tmp2(1),tmp2(2))
}
func makeSuffixFunc(suffix string) func(string) string {
return func(name string) string {
//判断名称是否包含后缀
if !strings.HasSuffix(name,suffix) {
return name+suffix
}
return name
}
}
func main() {
func1 := makeSuffixFunc(".bmp")
func2 := makeSuffixFunc(".jpg")
fmt.Println(func1("test"))
fmt.Println(func2("test"))
}
func calcs(base int) (func(int) int,func(int)int) {
add := func(i int) int{
base += i
return base
}
sub := func(i int) int{
base -= i
return base
}
return add,sub
}
func main() {
f1,f2 := calcs(10)
fmt.Println(f1(1),f2(2))
fmt.Println(f1(3),f2(4))
fmt.Println(f1(5),f2(6))
fmt.Println(f1(7),f2(8))
}
标签:例子 根据 func 任务 引用 hand 外部 def parameter
原文地址:https://blog.51cto.com/13812615/2479468