このビデオを見た後、自分で試してみました。ただし、パニックエラーが表示されますpanic: open templates/index.html: The system cannot find the path specified.
。完全なエラーメッセージは次のようなものです。
Hello, Go Web Development 1.3
panic: open templates/index.html: The system cannot find the path specified.
goroutine 1 [running]:
panic(0x789260, 0xc082054e40)
F:/Go/src/runtime/panic.go:481 +0x3f4
html/template.Must(0x0, 0xe34538, 0xc082054e40, 0x0)
F:/Go/src/html/template/template.go:340 +0x52
main.main()
E:/Users/User/Desktop/codespace/go_workspace/src/go-for-web-dev/src/1.3_UsingTemplate.go:11 +0x20d
"templates/index.html"
、、 ...のような別の文字列を試しました。また"index.html"
、テンプレート フォルダ全体をに"./template/index.html"
コピーしようとしましたが、同じエラー メッセージが表示されます。pkg
bin
以下は go プログラム (1.3_UsingTemplate.go) です。
package src
import (
"fmt"
"net/http"
"html/template"
)
func main() {
fmt.Println("Hello, Go Web Development 1.3")
templates := template.Must(template.ParseFiles("templates/index.html")) //This line should have some problem
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err := templates.ExecuteTemplate(w, "index.html", nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
fmt.Println(http.ListenAndServe(":8080",nil))
}
ファイル構造
アップデート
この問題を解決するには、まず現在の作業ディレクトリを *.go ファイルを含むフォルダーに変更する必要があります。次に、実行しgo run {filename.go}
ます。Run Configurations
GoClipse で、現在の作業ディレクトリを *.go ファイルを含むフォルダーに自動的に変更するための設定はありますか?