实验室7>使用Go语言提供接口【复制粘贴源码报错】

提问 iris  收藏
3 / 278

环境

go version:go version go1.14.2 darwin/amd64

代码

package main

import (
	"time"

	"github.com/iris-contrib/middleware/cors"
	"github.com/kataras/iris"
)

func main() {
	app := iris.New()

	// 跨域配置
	app.Use(cors.New(cors.Options{
		AllowedOrigins:   []string{"*"}, // allows everything, use that to change the hosts.
		AllowCredentials: true,
		MaxAge:           600,
		AllowedMethods:   []string{iris.MethodGet, iris.MethodPost, iris.MethodOptions, iris.MethodHead, iris.MethodDelete, iris.MethodPut},
		AllowedHeaders:   []string{"*"},
	}))
	app.AllowMethods(iris.MethodOptions)

	app.Get("/api/json", func(ctx iris.Context) {
		ctx.JSON(iris.Map{"curTime": time.Now()})
	})

	app.Run(iris.Addr(":80"), iris.WithoutServerError(iris.ErrServerClosed))
}

报错

# server
./main.go:14:18: cannot use cors.New(cors.Options literal) (type "github.com/kataras/iris/v12/context".Handler) as type "github.com/kataras/iris/context".Handler in argument to app.APIBuilder.Use
小码哥