154 lines
3.1 KiB
Go
154 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// 回调函数的类型定义
|
|
type Callback func(int) int
|
|
|
|
// 接受回调函数作为参数的函数
|
|
func process(nums []int, callback Callback) []int {
|
|
result := make([]int, len(nums))
|
|
for i, v := range nums {
|
|
result[i] = callback(v) // 调用回调函数
|
|
}
|
|
return result
|
|
}
|
|
|
|
// 模拟异步操作
|
|
func fetchData(callback func(string)) {
|
|
go func() {
|
|
time.Sleep(2 * time.Second)
|
|
data := "从服务器获取的数据"
|
|
callback(data) // 操作完成后调用回调
|
|
}()
|
|
}
|
|
|
|
// 事件处理器
|
|
type EventHandler struct {
|
|
callbacks map[string][]func(interface{})
|
|
}
|
|
|
|
func NewEventHandler() *EventHandler {
|
|
return &EventHandler{
|
|
callbacks: make(map[string][]func(interface{})),
|
|
}
|
|
}
|
|
|
|
// 注册事件回调
|
|
func (h *EventHandler) On(event string, callback func(interface{})) {
|
|
h.callbacks[event] = append(h.callbacks[event], callback)
|
|
}
|
|
|
|
// 触发事件
|
|
func (h *EventHandler) Emit(event string, data interface{}) {
|
|
if callbacks, exists := h.callbacks[event]; exists {
|
|
for _, callback := range callbacks {
|
|
callback(data) // 执行所有注册的回调
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
//// 定义回调函数
|
|
//square := func(x int) int {
|
|
// return x * x
|
|
//}
|
|
//nums := []int{1, 2, 3, 4, 5}
|
|
//result := process(nums, square)
|
|
//fmt.Println(result) // [1 4 9 16 25]
|
|
|
|
//fmt.Println("开始请求数据...")
|
|
//
|
|
//fetchData(func(data string) {
|
|
// fmt.Println("收到数据:", data)
|
|
//})
|
|
//
|
|
//time.Sleep(3 * time.Second) // 等待goroutine完成
|
|
|
|
//handler := NewEventHandler()
|
|
//
|
|
//// 注册点击事件回调
|
|
//handler.On("click", func(data interface{}) {
|
|
// fmt.Println("点击事件1:", data)
|
|
//})
|
|
//
|
|
//handler.On("click", func(data interface{}) {
|
|
// fmt.Println("点击事件2:", data)
|
|
//})
|
|
//
|
|
//// 触发事件
|
|
//handler.Emit("click", "按钮被点击")
|
|
|
|
// 协程返回值
|
|
//resultChan := make(chan int, 3)
|
|
//
|
|
//// 启动多个协程
|
|
//for i := 1; i <= 3; i++ {
|
|
// go worker(i, resultChan)
|
|
//}
|
|
//
|
|
//// 收集结果
|
|
//for i := 1; i <= 3; i++ {
|
|
// result := <-resultChan
|
|
// fmt.Printf("Received result: %d\n", result)
|
|
//}
|
|
|
|
// 带错误处理的返回值
|
|
values := []int{1, -2, 3, 4}
|
|
resultChan := make(chan Result, len(values))
|
|
|
|
for _, v := range values {
|
|
go asyncCalculate(v, resultChan)
|
|
}
|
|
|
|
for range values {
|
|
result := <-resultChan
|
|
if result.Err != nil {
|
|
fmt.Printf("Error: %v\n", result.Err)
|
|
} else {
|
|
fmt.Printf("Result: %d\n", result.Value)
|
|
}
|
|
}
|
|
}
|
|
|
|
type Result struct {
|
|
Value int
|
|
Err error
|
|
}
|
|
|
|
func calculate(x int) (int, error) {
|
|
if x < 0 {
|
|
return 0, errors.New("negative value not allowed")
|
|
}
|
|
return x * 2, nil
|
|
}
|
|
|
|
func asyncCalculate(x int, resultChan chan<- Result) {
|
|
value, err := calculate(x)
|
|
resultChan <- Result{Value: value, Err: err}
|
|
}
|
|
|
|
func worker(id int, resultChan chan<- int) {
|
|
fmt.Printf("Worker %d starting\n", id)
|
|
time.Sleep(time.Second)
|
|
fmt.Printf("Worker %d done\n", id)
|
|
resultChan <- id * 10 // 发送结果到通道
|
|
}
|
|
|
|
func TestAdd(t *testing.T) {
|
|
result := Add(2, 3)
|
|
expected := 5
|
|
if result != expected {
|
|
t.Errorf("Add(2, 3) = %d; want %d", result, expected)
|
|
}
|
|
}
|
|
|
|
func Add(a, b int) int {
|
|
return a + b
|
|
}
|