英文:
List the updated files without using go build again
问题
我刚开始学习Go语言。我有一个关于gorilla/mux的问题。
我正在尝试列出目录中的文件,并且响应将从GET请求发送回来列出这些文件。现在,当我在目录中创建一个新文件时,GET请求不会列出新文件。我理解我需要再次运行go build。我能否在不再次构建的情况下做到这一点?
type Images struct {
Image string `json:"image"`
Path string `json:"path"`
Timestamp string `json:"timestamp"`
Labels string `json:"labels"`
Version string `json:"version"`
}
var images []Images
func getImages(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(images)
}
func main() {
r := mux.NewRouter()
files, err := ioutil.ReadDir(os.Args[1])
if err != nil {
log.Fatal(err)
}
for _, file := range files {
images = append(images, Images{Image: file.Name(), Path: os.Args[1]})
}
fmt.Println(images)
// Route handles & endpoints
r.HandleFunc("/images", getImages).Methods("GET")
log.Fatal(http.ListenAndServe(":8080", r))
}
英文:
I am just starting with Go. I've a question regarding gorilla/mux.
I am trying to list out the files inside a directory; and the response will be sent back from GET request to list the files.
Now, when I create a new file inside directory; the GET request doesn't list the new file.
I understand I need to run go build again. Can I do it without building it again ?
type Images struct {
Image string `json:"image"`
Path string `json:"path"`
Timestamp string `json:"timestamp"`
Labels string `json:"labels"`
Version string `json:"version"`
}
var images []Images
func getImages(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(images)
}
func main() {
r := mux.NewRouter()
files, err := ioutil.ReadDir(os.Args[1])
if err != nil {
log.Fatal(err)
}
for _, file := range files {
images = append(images, Images{Image: file.Name(), Path: os.Args[1]})
}
fmt.Println(images)
// Route handles & endpoints
r.HandleFunc("/images", getImages).Methods("GET")
log.Fatal(http.ListenAndServe(":8080", r))
}
答案1
得分: 1
应用程序在启动时读取文件列表。要更新文件列表,必须重新启动应用程序。您不需要运行go build
来获取更新后的文件列表。
为了在每个请求中获取最新的文件列表,在请求处理函数中读取文件列表:
func getImages(w http.ResponseWriter, r *http.Request) {
files, err := ioutil.ReadDir(os.Args[1])
if err != nil {
http.Error(w, "Internal server error", 500)
return
}
var images []Images
for _, file := range files {
images = append(images, Images{Image: file.Name(), Path: os.Args[1]})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(images)
}
从main()
函数和images
包级变量中删除相应的代码。
英文:
The application reads the list of files at startup. To update the list of files, you must restart the application. You do not need to run go build
to get an updated list of files.
To get an up-to-date list of files on every request, read the list of files in the request handler function:
func getImages(w http.ResponseWriter, r *http.Request) {
files, err := ioutil.ReadDir(os.Args[1])
if err != nil {
http.Error(w, "Internal server error", 500)
return
}
var images []Images
for _, file := range files {
images = append(images, Images{Image: file.Name(), Path: os.Args[1]})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(images)
}
Remove the corresponding code from main()
and the images
package-level variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论