Search code examples
ubuntugowebgo-iris

iris (Go web framework) iris.PongoEngine error from iris official book


I started using Golang's Web Framework (Iris). I'm using the official iris book from gitbooks. I'm working on the last example in this page from the book. Following is the code used in the last example

./templates/hi.html

<!-- ./templates/hi.html -->
<html><head> <title> Hi Iris [THE TITLE] </title> </head>
  <body>
    <h1> Hi {{ Name }}
  </body>
</html>

./main.go

// ./main.go
import (
    "github.com/kataras/iris"
)

func main() {
    iris.Config.Render.Template.Engine = iris.PongoEngine
    iris.Get("/hi", hi)
    iris.Listen(":8080")
}

func hi(ctx *iris.Context){
   ctx.Render("hi.html", map[string]interface{}{"Name": "iris"})
}

When I run the main.go, following are the errors I got.

# command-line-arguments
./main.go:8: iris.Config.Render undefined (type *config.Iris has no field or method Render)
./main.go:8: undefined: iris.PongoEngine

I followed all the steps correctly, and also downloaded all the dependencies. I already took Learn How To Code: Google's Go (golang) Programming Language - Udemy and Golang Workshop by Caleb Doxcy, so I know the basics, like how to install dependencies, and how to import them etc. But the example shown in the book doesn't work.


Solution

  • import "github.com/kataras/iris/v12"
    
    func main() {
    
        app := iris.New()
        app.RegisterView(iris.Django("./templates", ".html")) // <-----
    
        // RESOURCE: http://127.0.0.1:8080/hi
        // METHOD: "GET"
        app.Get("/hi", hi)
    
        app.Listen(":8080")
    }
    
    func hi(ctx iris.Context){
       ctx.ViewData("Name", "iris")
       ctx.View("hi.html")
    }