当前位置: 首页 > wzjs >正文

上海将打造五大未来产业集群淘宝关键词优化软件

上海将打造五大未来产业集群,淘宝关键词优化软件,html5 手机网站开发叫才,万网备案初审过了后网站能访问吗Go 语言函数类型的巧妙应用 函数类型在 Go 语言中非常强大,允许将函数作为值进行传递和操作。下面详细介绍函数类型的各种妙用: 1. 回调函数 // 定义一个函数类型 type Callback func(int) int// 接受回调函数的函数 func processData(data []int, ca…

Go 语言函数类型的巧妙应用

函数类型在 Go 语言中非常强大,允许将函数作为值进行传递和操作。下面详细介绍函数类型的各种妙用:

1. 回调函数

// 定义一个函数类型
type Callback func(int) int// 接受回调函数的函数
func processData(data []int, callback Callback) []int {result := make([]int, len(data))for i, v := range data {result[i] = callback(v)}return result
}// 使用示例
func main() {numbers := []int{1, 2, 3, 4, 5}// 使用匿名函数作为回调doubled := processData(numbers, func(x int) int {return x * 2})// 使用已定义函数作为回调squared := processData(numbers, square)fmt.Println(doubled)  // [2 4 6 8 10]fmt.Println(squared)  // [1 4 9 16 25]
}func square(x int) int {return x * x
}

2. 策略模式实现

type PaymentStrategy func(amount float64) boolfunc processPayment(amount float64, strategy PaymentStrategy) bool {return strategy(amount)
}// 各种支付策略
func creditCardPayment(amount float64) bool {// 信用卡支付逻辑return true
}func alipayPayment(amount float64) bool {// 支付宝支付逻辑return true
}// 使用示例
func pay(amount float64, paymentMethod string) bool {switch paymentMethod {case "credit":return processPayment(amount, creditCardPayment)case "alipay":return processPayment(amount, alipayPayment)default:return false}
}

3. 装饰器模式

type HttpHandler func(w http.ResponseWriter, r *http.Request)// 日志装饰器
func LoggingDecorator(handler HttpHandler) HttpHandler {return func(w http.ResponseWriter, r *http.Request) {fmt.Printf("Request: %s %s\n", r.Method, r.URL.Path)handler(w, r)fmt.Println("Request completed")}
}// 认证装饰器
func AuthDecorator(handler HttpHandler) HttpHandler {return func(w http.ResponseWriter, r *http.Request) {// 检查认证信息if authenticate(r) {handler(w, r)} else {http.Error(w, "Unauthorized", http.StatusUnauthorized)}}
}// 使用装饰器
func main() {http.HandleFunc("/api/data", LoggingDecorator(AuthDecorator(handleData)))http.ListenAndServe(":8080", nil)
}func handleData(w http.ResponseWriter, r *http.Request) {// 业务逻辑
}

4. 函数选项模式

type Server struct {host stringport inttimeout time.DurationmaxConn int
}type ServerOption func(*Server)// 创建Server的选项函数
func WithHost(host string) ServerOption {return func(s *Server) {s.host = host}
}func WithPort(port int) ServerOption {return func(s *Server) {s.port = port}
}func WithTimeout(timeout time.Duration) ServerOption {return func(s *Server) {s.timeout = timeout}
}func WithMaxConn(maxConn int) ServerOption {return func(s *Server) {s.maxConn = maxConn}
}// 创建服务器
func NewServer(options ...ServerOption) *Server {// 设置默认值server := &Server{host:    "localhost",port:    8080,timeout: 30 * time.Second,maxConn: 100,}// 应用所有选项for _, option := range options {option(server)}return server
}// 使用示例
func main() {server := NewServer(WithHost("example.com"),WithPort(9000),WithTimeout(60 * time.Second),)// 使用server...
}

5. 中间件链

type Middleware func(http.Handler) http.Handler// 中间件链
func Chain(middlewares ...Middleware) Middleware {return func(next http.Handler) http.Handler {for i := len(middlewares) - 1; i >= 0; i-- {next = middlewares[i](next)}return next}
}// 使用示例
func main() {handler := http.HandlerFunc(finalHandler)// 创建中间件链chain := Chain(loggingMiddleware,authMiddleware,rateLimitMiddleware,)// 应用中间件链http.Handle("/api", chain(handler))http.ListenAndServe(":8080", nil)
}

6. 延迟执行与钩子函数

type ShutdownHook func()type App struct {shutdownHooks []ShutdownHook
}func (a *App) AddShutdownHook(hook ShutdownHook) {a.shutdownHooks = append(a.shutdownHooks, hook)
}func (a *App) Shutdown() {// 按照注册顺序的相反顺序执行钩子for i := len(a.shutdownHooks) - 1; i >= 0; i-- {a.shutdownHooks[i]()}
}// 使用示例
func main() {app := &App{}// 注册数据库关闭钩子app.AddShutdownHook(func() {fmt.Println("关闭数据库连接")})// 注册文件清理钩子app.AddShutdownHook(func() {fmt.Println("清理临时文件")})// 应用运行...// 关闭应用app.Shutdown()
}

7. 操作集合的函数

type FilterFunc func(int) bool
type MapFunc func(int) int
type ReduceFunc func(int, int) int// 过滤集合
func Filter(nums []int, filter FilterFunc) []int {result := []int{}for _, n := range nums {if filter(n) {result = append(result, n)}}return result
}// 映射集合
func Map(nums []int, mapper MapFunc) []int {result := make([]int, len(nums))for i, n := range nums {result[i] = mapper(n)}return result
}// 归约集合
func Reduce(nums []int, initialValue int, reducer ReduceFunc) int {result := initialValuefor _, n := range nums {result = reducer(result, n)}return result
}// 使用示例
func main() {numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}// 过滤偶数evens := Filter(numbers, func(n int) bool {return n%2 == 0})// 将数字翻倍doubled := Map(evens, func(n int) int {return n * 2})// 求和sum := Reduce(doubled, 0, func(acc, n int) int {return acc + n})fmt.Println("结果:", sum) // 60
}

8. 依赖注入

type UserRepository interface {FindByID(id int) (User, error)
}type UserService struct {repo UserRepository
}func NewUserService(repo UserRepository) *UserService {return &UserService{repo: repo}
}// 测试时可以轻松注入模拟实现
func TestUserService(t *testing.T) {mockRepo := &MockUserRepository{FindByIDFunc: func(id int) (User, error) {return User{ID: id, Name: "测试用户"}, nil},}service := NewUserService(mockRepo)// 测试 service...
}

9. 自定义排序

type Person struct {Name stringAge  int
}type SortBy func(p1, p2 *Person) booltype PersonSorter struct {people []Personless   SortBy
}func (s PersonSorter) Len() int           { return len(s.people) }
func (s PersonSorter) Swap(i, j int)      { s.people[i], s.people[j] = s.people[j], s.people[i] }
func (s PersonSorter) Less(i, j int) bool { return s.less(&s.people[i], &s.people[j]) }// 使用示例
func main() {people := []Person{{"张三", 30},{"李四", 25},{"王五", 35},}// 按年龄排序sort.Sort(PersonSorter{people: people,less: func(p1, p2 *Person) bool {return p1.Age < p2.Age},})fmt.Println("按年龄排序:", people)// 按姓名排序sort.Sort(PersonSorter{people: people,less: func(p1, p2 *Person) bool {return p1.Name < p2.Name},})fmt.Println("按姓名排序:", people)
}

10. 惰性计算

type LazyEval func() interface{}func computeExpensiveValue() LazyEval {computed := falsevar result interface{}return func() interface{} {if !computed {fmt.Println("执行昂贵计算...")// 模拟耗时操作time.Sleep(1 * time.Second)result = 42computed = true}return result}
}// 使用示例
func main() {// 创建惰性计算lazy := computeExpensiveValue()fmt.Println("惰性计算创建后,尚未执行计算")// 调用时才执行实际计算value := lazy()fmt.Println("第一次获取值:", value)// 再次调用不会重复计算value = lazy()fmt.Println("第二次获取值:", value)
}

函数类型使 Go 拥有了函数式编程的部分能力,同时保持了语言的简洁性和性能,这使得它在构建灵活、可测试和可维护的代码时非常有价值。

http://www.dtcms.com/wzjs/109493.html

相关文章:

  • 作文大全网站网络工程师培训班要多少钱
  • 专业做家电经销的网站资源搜索
  • 做美团类似的网站爱链在线
  • 淘宝客app开发公众号排名优化软件
  • 福州网站建设万网注册域名
  • 三元爱力优网站建设日期今日小说排行榜风云榜
  • 中山做网站做的好的公司成免费crm特色
  • 深圳全网推广营销怎么做武汉网站建设优化
  • 企腾网络推广效果怎么样seo 0xu
  • 衢州网站建设公司百度seo关键词怎么做
  • 房产中介网站建设技巧正规代运营公司
  • 上海网站建设公司大全聚名网域名注册
  • 建筑网站建设公司网站推广是做什么的
  • 门户网站建设模板下载深圳网站建设开发公司
  • php做网站难么陕西百度推广的代理商
  • 网站建设怎样上传程序我想做网络推广
  • 上海市建设管理委员会网站中国教师教育培训网
  • 陕西省西安市网站建设公司平台连接
  • 上海网站建设基础百度识图网页版
  • 天天向上做图网站关键词seo排名
  • 成都企业做网站google 网站推广
  • 做电商网站一般需要什么流程成都网站建设方案托管
  • 网站开发与维护的岗位特点职责seo培训中心
  • 奉贤长沙网站建设网页制作软件dw
  • 旅游做攻略用什么网站线上电脑培训班
  • 云建造网站模板网站
  • 专门做游戏的网站西安百度搜索排名
  • 永久域名购买杭州seo关键字优化
  • 公众号链接的手机网站怎么做的百度竞价排名价格查询
  • 做网站的介绍推广排名seo