Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 大猩猩多路复用器404_Go_Mux - Fatal编程技术网

Go 大猩猩多路复用器404

Go 大猩猩多路复用器404,go,mux,Go,Mux,我这次休假是为了在旅途中梳洗一下。不幸的是,我下面的代码在两条路径上都抛出了404。这是最新的迭代。我最初把路由器放在一个handleRouter功能中,并认为把它拿出来可以修复404的问题。剧透警报:它没有。我怎样才能解决这个问题?谢谢 package main import ( "encoding/json" "fmt" "log" "net/http" "github.com/gorilla/mux" ) type Article struct

我这次休假是为了在旅途中梳洗一下。不幸的是,我下面的代码在两条路径上都抛出了404。这是最新的迭代。我最初把路由器放在一个handleRouter功能中,并认为把它拿出来可以修复404的问题。剧透警报:它没有。我怎样才能解决这个问题?谢谢

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

type Article struct {
    Title   string `json:"Title"`
    Desc    string `json:"desc"`
    Content string `json:"content"`
}

type Articles []Article

func main() {
    fmt.Println("Router v2 - Muxx")

    myRouter := mux.NewRouter()
    myRouter.HandleFunc("/all", returnAllArticles).Methods("GET")
    myRouter.HandleFunc("/", homePage).Methods("GET")
    log.Fatal(http.ListenAndServe(":8000", nil))
}

func homePage(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello:")
    fmt.Println("Endpoint Hit: homepage")
}

func returnAllArticles(w http.ResponseWriter, r *http.Request) {
    articles := Articles{
        Article{Title: "Hello", Desc: "Article Description", Content: "Article Content"},
        Article{Title: "Hello 2", Desc: "Article Description", Content: "Article Content"},
    }

    fmt.Println("Endpoint Hit: returnAllArticles")
    json.NewEncoder(w).Encode(articles)

}

要使用路由器,必须将其传递到HTTP服务器

log.Fatal(http.ListenAndServe(":8000", myRouter))
或使用默认的serve mux进行注册:

http.Handle("/", myRouter)
log.Fatal(http.ListenAndServe(":8000", nil))

您正在调用http.listendServe
http.listendServe,但实际上没有将其传递给路由器。请参阅中第一个示例中的
http.Handle(“/”,r)
。为什么要进行向下投票?这似乎是一个很好的问题…
http.Handle(“/api”,router1)
-->找不到?--如何找到此
http.Handle(“/”,route2)
-->成功