7

このビデオを見た後、自分で試してみました。ただし、パニックエラーが表示されます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"コピーしようとしましたが、同じエラー メッセージが表示されます。pkgbin

以下は 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 ConfigurationsGoClipse で、現在の作業ディレクトリを *.go ファイルを含むフォルダーに自動的に変更するための設定はありますか?

4

2 に答える 2

4

現在の作業ディレクトリからの相対パスを指定しました。このディレクトリは、ソース コードを含むディレクトリとは関係がない場合があります。

ディレクトリをに変更しE:/Users/User/Desktop/codespace/go_workspace/src/go-for-web-dev/srcて、プログラムを実行します。テンプレートへのパスは、このディレクトリからの相対パスです。

于 2016-05-19T16:30:36.277 に答える