[Solved] unable to test template in golang


If you want to test a method that contains template which is executing inside that particular method then you have to provide the absolute path.

Follow these steps:

  1. Set current working directory in your terminal:

    export varname="absolutepath"
    

    For example:

    export PATH="/go/src/github.com/project1"
    
  2. Use this command in your terminal:

    echo $varname
    

    For example:

    echo $PATH
    /go/src/github.com/project1
    

    It will give you full path that you have set.

  3. In your program use this:

    path := os.Getenv("PATH")
    t, err := template.ParseFiles(Path + "views/dockerconfig.gtpl")
    if err != nil {
        fmt.Println(errors.New("unable to execute the template"))
    }
    t.Execute(w, nil)
    

4

solved unable to test template in golang