Answer the question
In order to leave comments, you need to log in
How is a file path formed in a Go application when working with "html/template" templates?
learning to write in Go.
I took this article as a basis https://habr.com/ru/post/475390/
but I pack it in docker.
I get the following error when compiling:
...
Recreating go-hello ... done
Attaching to go-hello
go-hello | panic: open index.html: no such file or directory
go-hello |
go-hello | goroutine 1 [running]:
go-hello | html/template.Must(...)
go-hello | /usr/local/go/src/html/template/template.go:374
go-hello | main.init()
go-hello | /goapp/main.go:9 +0x8c
go-hello exited with code 2
## ./app/main.go
---
package main
import (
"html/template"
"net/http"
"os"
)
var tpl = template.Must(template.ParseFiles("index.html"))
func indexHandler(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("<h1>Hello World!</h1>"))
tpl.Execute(w, nil)
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandler)
http.ListenAndServe(":"+port, mux)
}
---
---
---
## ./app/index.html
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>News App Demo</title>
<link rel="stylesheet" href="/assets/style.css">
</head>
<body>
<h1>News App Demo</h1>
</body>
</html>
---
---
---
## ./Dockerfile
---
# syntax=docker/dockerfile:1
FROM golang:1.18-buster as builder
WORKDIR /goapp
COPY app/* ./
RUN go mod download
RUN CGO_ENABLED=0 go build -o /hello-go
FROM alpine:3.14
WORKDIR /
COPY --from=builder /hello-go /hello-go
EXPOSE 8080
HEALTHCHECK --interval=5s --timeout=10s --retries=3 CMD curl -sS 127.0.0.1:8080 || exit 1
ENTRYPOINT ["/hello-go"]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question