go func 使用

  • 2022-07-09
  • 浏览 (971)

golang func 使用样例

package main

import (
	"fmt"
	"testing"
)

func Add(a int, b int) int {
	return a + b
}

func TestAdd(t *testing.T) {
	fmt.Println(Add(1, 8))
}

// 声明一个函数类型
type cb func(int) int

func callback(x int, f cb) {
	f(x)
}

func TestCallback(t *testing.T) {
	callback(5, func(x int) int {
		fmt.Printf("call back value is %d \n", x)
		return x
	})
}

测试

执行测试代码 go test -v func_test.go 可以查看执行结果

go test 使用请看:go test 使用

golang 使用样例汇总:go test

你可能感兴趣的文章

go array 使用

go defer 使用

go file 使用

go http 使用

go interface 使用

go main 使用

go map 使用

go math 使用

go method 使用

go range 使用

0  赞