Problems running binary created by Go build - go

I have a simple Go application, it has a few template files where I render some text. After I build my binary with Go build I try to run the file and I get the error:
panic: html/template: pattern matches no files: public/*.html
I am using the Echo framework and have followed their steps on adding a render for templates.
Here is the code in my main.go file
// TemplateRenderer is a custom html/template renderer for Echo framework
type TemplateRenderer struct {
templates *template.Template
}
// Render renders a template document
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
// Create a new instance of Echo
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
renderer := &TemplateRenderer{
templates: template.Must(template.ParseGlob("public/*.html")),
}
e.Renderer = renderer
e.GET("/", func(context echo.Context) error {
return context.Render(http.StatusOK, "index.html", api.FetchCoinList())
})
}
Is there something I have to do to package my templates up in the binary? It works perfectly when I run go run main.go

Is there something I have to do to package my templates up in the binary?
Yes, make them avialable at the same (relative) folder where they are present when you run them with go run main.go.
For example if there is a public folder containing the templates next to your main.go, then make sure you "copy" the public folder next to your executable binary.
Read this question+answer for more options: how to reference a relative file from code and tests
Usually you should provide ways to define where to get static assets and files from. The app may have a default place to look for them, but it should be easy to change this setting (e.g. via command line flags, via environment variables or via config files).
Another option is to include static files in the executable binary. Check out this question how to do that: What's the best way to bundle static resources in a Go program?

Related

passing named parameters to index template while using httprouter golang package

I just learnt how to use the httprouter go package and read many documents about it but
failed to use the :name style of passing paramenters toe templates when it comes to the index
page template.
ex.
My router code:
func getRouter() *httprouter.Router {
// Load and parse templates (from binary or disk)
templateBox = rice.MustFindBox("templates")
templateBox.Walk("", newTemplate)
// mux handler
router := httprouter.New()
// Example route that encounters an error
router.GET("/broken/handler", broken)
// Index route
router.GET("/:email", index)
router.GET("/read/all", readingHandler)
router.POST("/submit/newcon", Handler1)
router.POST("/read/newcon", Handler2)
// Serve static assets via the "static" directory
fs := rice.MustFindBox("static").HTTPBox()
router.ServeFiles("/static/*filepath", fs)
return router
}
Then i get this error:
panic: wildcard segment ':email' conflicts with existing children in path '/:email'
so it should work with /user/:email instead of just /:email.

Golang best way of calling/invoking sub project

Im moving my project from PHP to Golang and I looking efficient way of calling/invoking/handling control to sub project main.go from src main.go, I want to pass control from
http://localhost/ => http://localhost/sub-project1/
http://localhost/ => http://localhost/sub-project2/
http://localhost/ => http://localhost/sub-projectn/
I'm new to Golang I don't know how to do it in best way, and my project structure is
src/
main.go
sub-project1/
main.go
sub-project2/
main.go
sub-projectn/
main.go
gitHub.com/
......
golang.org/
......
I'm using httprouter for routing, in main.go which is located under src contain following
package main
import ....
// homePageHandler
// contactPageHandler
// aboutPageHandler
// loginPageHandler
// signupPageHandler
func main() {
router := httprouter.New()
router.GET("/", homePageHandler)
router.GET("/contact", contactPageHandler)
router.GET("/about", aboutPageHandler)
router.GET("/login", loginPageHandler)
router.GET("/signup", signupPageHandler)
// here I want to pass control to my sub project main.go
// and I don't want to write any /sub-project routing urls here,
// because each /sub-project's contain many urls
router.GET("/sub-project1", ??????)
router.GET("/sub-project2", ??????)
router.GET("/sub-project3", ??????)
router.GET("/sub-projectn", ??????)
}
and all files must be passes from src main.go because whole project has only one main() and inside any /sub-projectx main.go I want to do this
package main
import ....
// subprojectPageHandler
// feature1PageHandler
// feature2PageHandler
// feature3PageHandler
// ........
// featurenPageHandler
func main() {
router := httprouter.New()
router.GET("/sub-projectx", subprojectPageHandler)
router.GET("/sub-projectx/feature1", feature1PageHandler)
router.GET("/sub-projectx/feature2", feature2PageHandler)
router.GET("/sub-projectx/feature3", feature3PageHandler)
..........
router.GET("/sub-projectx/featureN", featureNPageHandler)
// here I want to pass control to my sub project main.go
// and I don't want to write any /sub-project routing urls here,
// because each /sub-project's contain many urls
router.GET("/sub-project1", ??????)
router.GET("/sub-project2", ??????)
router.GET("/sub-project3", ??????)
router.GET("/sub-projectn", ??????)
}
To be
Golang source code is not running through interpreter, but is built into a binary, which gives less flexibility in case of dynamic projects. That said, I'd keep my projects isolated one from another, and would let Nginx (for example) take care of multiple project grouping. Of course, that would require some refactoring like creating shared packages, etc.
Or not to be
If, for some reason, you still think running multiple projects via single binary is ok, it's your choice. In this case you might have a look into route grouping that are available in some frameworks. Here's what Go Gin provides:
func main() {
router := gin.Default()
// Simple group: v1
v1 := router.Group("/v1")
{
v1.POST("/login", loginEndpoint)
v1.POST("/submit", submitEndpoint)
v1.POST("/read", readEndpoint)
}
// Simple group: v2
v2 := router.Group("/v2")
{
v2.POST("/login", loginEndpoint)
v2.POST("/submit", submitEndpoint)
v2.POST("/read", readEndpoint)
}
router.Run(":8080")
}

How to make templates work with gin framework?

I am newbie to golang.
To learn it I have started with a simple web app using gin framework.
I have followed the gin doc & configured template file but not able to make it work. I am getting an error -
panic: html/template: pattern matches no files: `templates/*`
goroutine 1 [running]:
html/template.Must
/usr/local/Cellar/go/1.5.2/libexec/src/html/template/template.go:330
github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob
/Users/ameypatil/deployment/go/src/github.com/gin-gonic/gin/gin.go:126
main.main()
/Users/ameypatil/deployment/go/src/github.com/ameykpatil/gospike/main.go:17
Below is my code -
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
//os.Setenv("GIN_MODE", "release")
//gin.SetMode(gin.ReleaseMode)
// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()
router.LoadHTMLGlob("templates/*")
//router.LoadHTMLFiles("templates/index.tmpl")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "GoSpike",
})
})
// By default it serves on :8080 unless a
// PORT environment variable was defined.
router.Run(":4848")
}
My directory structure is
- gospike
--- templates
------index.tmpl
--- main.go
go install command does not give any error
but on actually running, it gives the above error. I searched & there were similar issues logged on gin's github repo but they are closed now.
I have tried various things but I guess I am missing something obvious. What am I missing?
I'm guessing the issue is that you're using a relative filepath to access your templates.
If I compile and run your code from the gospike directory, it works fine. But if I run gospike from any other directory, I get the same error you were seeing.
So either you need to always run gospike in the parent directory of templates, or you need to use the absolute path. You could either hard code it:
router.LoadHTMLGlob("/go/src/github.com/ameykpatil/gospike/templates/*")
or you could do something like
router.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"),
"src/github.com/ameykpatil/gospike/templates/*"))
but that will fail if you have multiple paths set in your GOPATH. A better long-term solution might be setting a special environment variable like TMPL_DIR, and then just using that:
router.LoadHTMLGlob(filepath.Join(os.Getenv("TMPL_DIR"), "*"))
use relative path glob will working, you can try to code
router.LoadHTMLGlob("./templates/*")
notice the . dot sign which meaning current directory, gin.Engine will load template
base on templates subdirectory of current directory .
Here is how I do it. This walks through the directory and collects the files marked with my template suffix which is .html & then I just include all of those. I haven't seen this answer anywhere so I thought Id post it.
// START UP THE ROUTER
router := gin.Default()
var files []string
filepath.Walk("./views", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".html") {
files = append(files, path)
}
return nil
})
router.LoadHTMLFiles(files...)
// SERVE STATICS
router.Use(static.Serve("/css", static.LocalFile("./css", true)))
router.Use(static.Serve("/js", static.LocalFile("./js", true)))
router.Use(static.Serve("/images", static.LocalFile("./images", true)))
routers.LoadBaseRoutes(router)
routers.LoadBlog(router)
router.Run(":8080")
There is a multitemplate HTML render to support multi tempaltes.
You can use AddFromFiles to add multi files:
r.AddFromFiles("index", "templates/base.html", "templates/index.html")
r.AddFromFiles("article", "templates/base.html", "templates/index.html", "templates/article.html")
Or load multi files under a directory:
package main
import (
"path/filepath"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.New()
r.HTMLRender = loadTemplates()
// ...
}
func loadTemplates() multitemplate.Render {
files, err := filepath.Glob("template/*.tmpl")
if err != nil {
panic("failed to load html templates: " + err.Error())
}
r := multitemplate.New()
// Generate our templates map from our template/ directories
for _, file := range files {
r.AddFromFiles(filepath.Base(file), file)
}
// add other html templates directly
r.Add("test.tmpl", someTemplate)
return r
}
You could see more APIs in this repo you want, hope this post help :)

Package is found but contents are not?

I get a strange error while building my go project.
My structure:
-$GOPATH
-src
-main
-main.go
-configuration
-configuration.go
configuration.go:
package configuration;
type Config int;
func (c Config) Parse(s string) map[string]string {...}
main.go
package main;
import"configuration"
func main() {
var config Config;
argMap := config.parse(...);
return;
}
if my working directory is $GOPATH, I do:
go build configuration - no output, OK
go build main
imported and not used "configuration"
undefined: Config
So my package is found ($GOPATH/pkg contains configuration.go with correct content - I can see the Parse method) and main imports it, but does not recognize its contents?
I recon the problem is that the type Config is not exported? Why would that be?
You are trying to use Config from package main, where it is not defined, instead of the one from configuration (thats the error "imported and not used"):
package main
import "configuration"
func main() {
var config configuration.Config
argMap := config.Parse(...)
}
The second problem is calling unexported parse instead of Parse as explained by VonC.
argMap := config.parse(...); wouldn't work, since you declared a Parse() method.
(as in "exported method configuration.Parse()")
var config configuration.Config
argMap := config.Parse(...);
Config is exported, but the methods are case-sensitive (cf. Exported Identifiers).

How to use other struct methods from other files in Golang

I have a file called login.go and account.go
In login.go
func (api *ApiResource) test() {
fmt.Println("Works!")
}
In account.go I have:
func main () {
Res := new(ApiResource)
Res.test()
}
Buit I'm getting undefined:test error.
They both use package main and are on same src/ folder
What do I need to fix here?
If you used go run then you must pass both files to like go run login.go account.go.

Resources