0

这是我的代码

`package main 
 import "github.com/kataras/iris"
 func main()    {
     iris.Get("/hi",    func(ctx    *iris.Context)  {
         ctx.Writef("Hi %s",    "iris")
     })
     iris.Listen(":8080")
 }`

我有“go get -u github.com/kataras/iris/iris”这就是我得到的,我一直在生存,但仍然无法解决这个问题。

./IRIS.go:6: undefined: iris.Get
./IRIS.go:9: undefined: iris.Listen

这是我第一次尝试这个框架,我从页面https://docs.iris-go.com/开始 我虽然这很容易,但实际上并不容易,我只能安装 iris,这是我第一次更糟糕的你好世界

我正在使用 Intellij Idea 作为我的 IDE

请帮我。谢谢

4

2 回答 2

0

这很有趣,因为https://github.com/kataras/irishttps://iris-go.comhttps://docs.iris-go.com有很多这样的例子,你会花时间问那,您可能检查了 Iris 的第三方分支,而不是 Iris 本身您在哪里找到该代码片段?

正确的方法:

package main 
import "github.com/kataras/iris"
func main()    {
    app := iris.New()
    app.Get("/hi", func(ctx iris.Context)  { // go > 1.9
        ctx.Writef("Hi %s", "iris")
    })
    app.Run(iris.Addr(":8080"))
}

这是另一个示例,一个简单的服务器,它向客户端呈现带有消息的模板文件:

// file: main.go
package main
import "github.com/kataras/iris"

func main() {
    app := iris.New()
    // Load all templates from the "./views" folder
    // where extension is ".html" and parse them
    // using the standard `html/template` package.
    app.RegisterView(iris.HTML("./views", ".html"))

    // Method:    GET
    // Resource:  http://localhost:8080
    app.Get("/", func(ctx iris.Context) {
        // Bind: {{.message}} with "Hello world!"
        ctx.ViewData("message", "Hello world!")
        // Render template file: ./views/hello.html
        ctx.View("hello.html")
    })

    // Method:    GET
    // Resource:  http://localhost:8080/user/42
    app.Get("/user/{id:long}", func(ctx iris.Context) {
        userID, _ := ctx.Params().GetInt64("id")
        ctx.Writef("User ID: %d", userID)
    })

    // Start the server using a network address.
    app.Run(iris.Addr(":8080"))
}

<!-- file: ./views/hello.html -->
<html>
<head>
    <title>Hello Page</title>
</head>
<body>
    <h1>{{.message}}</h1>
</body>
</html>

$ go run main.go
> Now listening on: http://localhost:8080
> Application started. Press CTRL+C to shut down.

如果您需要有关 Iris 的任何帮助,您可以随时通过实时聊天https://chat.iris-go.com与支持团队联系。祝你今天过得愉快!

于 2017-09-10T10:23:06.533 回答
0

如果 Go 1.8 仍然是你的 Go 应用程序的基本宿主,那么你应该在源文件的 imports 语句中声明并使用 github.com/kataras/iris/context 包。

package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/context"
)

func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))

app.Get("/", func(ctx context.Context) {
    ctx.ViewData("message", "Hello world!")
    ctx.View("hello.html")
})

app.Run(iris.Addr(":8080"))
}
于 2017-09-15T04:21:15.693 回答