2017-03-20 74 views
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") 
}` 

我「去找-u github.com/kataras/iris/iris」 這是我得到的,我一直在仍然無法解決這個問題。

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

這是我第一次嘗試這樣的框架,我也跟着從https://docs.iris-go.com/頁 我雖然它會很容易,但它真的不行,我唯一能做的安裝虹膜,這是我第一次更糟糕的Hello World

我使用的IntelliJ IDEA作爲我的IDE

請幫助我。由於

+2

有一個在庫中的Hello World示例:https://github.com/kataras/iris/blob/v6/_examples/examples/hello-world/main.go - 雖然我建議你先一個更好的教程:https://golang.org/doc/articles/wiki/ - 這將減少以後的混淆。 – elithrar

+0

來自golang文檔的hello world不存在疑問,但虹膜文檔不能正常工作 btw,謝謝你的回答,也許這個年齡段的文檔不能再工作 –

+2

@BudayaLanu:如果你剛剛入門,會避免虹膜([如果可能,我實際上完全避免虹膜](http://www.florinpatan.ro/2016/10/why-you-should-not-use-iris-for-your-go.html)) 。從std庫中的基礎開始,從std文檔開始:[如何編寫Go代碼](https://golang.org/doc/code.html)。 – JimB

回答

0

這很有趣,因爲https://github.com/kataras/irishttps://iris-go.comhttps://docs.iris-go.com有這些東西的例子很多,你花你的時間問的是,你可能檢查虹膜的第三方叉,而不是光圈本身你在哪裏找到的代碼片段?

正確的方法:

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與支持團隊聯繫。祝你今天愉快!

0

如果Go 1.8仍然是您的應用程序的基本主機,那麼您應該在源文件的import語句中聲明並使用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")) 
} 
相關問題