繁体   English   中英

虹膜错误-未解决的“获取”

[英]Iris Error - Unresolved 'Get'

这是我的代码

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

我已经“得到-u github.com/kataras/iris/iris”,这就是我得到的,而且我一直在努力,仍然无法解决这个问题。

./IRIS.go:6:未定义:iris.Get
./IRIS.go:9:未定义:iris.Listen

这是我第一次尝试此框架,我从页面https://docs.iris-go.com/跟随,尽管这很容易,但实际上并非如此,我只能安装iris,这是我的第一个更糟糕的问候世界

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

请帮我。 谢谢

这很有趣,因为https://github.com/kataras/iris以及https://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与支持团队联系。 祝你今天愉快!

如果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"))
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM