golang 结构体中的匿名方法字段

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wide288/article/details/84336594
package service

import "fmt"

type Command struct {
	// Run runs the command.
	// The args are the arguments after the command name.
	Run func(args string) int

	// PreRun performs an operation before running the command
	PreRun func(args string)

	// UsageLine is the one-line Usage message.
	// The first word in the line is taken to be the command name.
	UsageLine string
}

var com = &Command{
	Run:       func(args string) int { return 11 },
	PreRun:    func(args string) { PreRun2("22") },
	UsageLine: "2333",
}

// 调用此方法做入口
func TestStructMethod2() {
	// obj1 := new(Command)
	// com2 := new(Command{
	// 	Run2("322"),
	// 	PreRun2("22"),
	// 	"300",
	// })
	fmt.Println("var val:", com)
}

func Run2(args string) int {
	fmt.Println("run2:", args)
	return 32
}

func PreRun2(args string) {
	fmt.Println("PreRun2:", args)
	fmt.Println("version: 1.0")
}

从 bee 代码中简化来的,写的还不够好。bee 中是查找参数对应的处理方法,这里没有体现用法。

好在是编译通过的。

输出是:

猜你喜欢

转载自blog.csdn.net/wide288/article/details/84336594