Comments (3)
You're getting undefined
errors because your program doesn't know what mux.HandleFunc
is. Note that HandleFunc
is a function of the Router
method—so mux.HandleFunc
doesn't refer to anything.
If you take a look at the first example on http://www.gorillatoolkit.org/pkg/mux you'll find the following:
func main() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/products", ProductsHandler)
r.HandleFunc("/articles", ArticlesHandler)
http.Handle("/", r)
}
You need to create Router with mux.NewRouter
and pass the router to http.Handle
so it can offload your routes to the mux router.
Re-write your main
function so it looks like this:
func main() {
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r http.Request) { fmt.Fprint(w, "Hello")})
http.Handle("/", r)
http.ListenAndServe(":10000", nil)
}
That should solve the problem.
from mux.
You need to set GOPATH correctly. Please review
http://golang.org/doc/code.html
On 10/06/2013, at 14:17, Matthew Taylor [email protected] wrote:
I have a very simple go file, and when I try to build, i get this:
go build
_/home/matt/projects/goprojects/muxHello
./main.go:10: undefined: mux.HandleFunc
./main.go:14: undefined: mux.DefaultRoute
here is my code:package main
import (
"fmt"
"github.com/gorilla/mux"
"net/http"
)func main() {
mux.HandleFunc("/", func(w http.ResponseWriter, r http.Request) {
fmt.Fprint(w, "Hello")
})http.Handle("/", mux.DefaultRoute) http.ListenAndServe(":10000", nil)
}
I set my GOPATH to /home/matt/projects/goprojects/muxHello
then dogo get github.com/gorilla/mux
and the folders show up in/home/matt/projects/goprojects/muxHello/src/github.com/gorilla/mux
/home/matt/projects/goprojects/muxHello/src/github.com/gorilla/context
I've tried other imports and they have worked. for instance, https://code.google.com/p/goauth2/ and https://github.com/eaigner/jet both work using the exact same steps I took with gorilla/mux.It's like it is finding the package; but the package isn't exporting anything.
here is the output of "go env"
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/matt/projects/goprojects/muxHello"
GORACE=""
GOROOT="/home/matt/go"
GOTOOLDIR="/home/matt/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"
I installed go from source. I used the release branch of go 1.1.go version go1.1 linux/amd64
I am building a restful api as my first go project. I apologize in advance if this is because of a mistake I've made or if this isn't the right "forum" for this. I have spent several hours trying to make sure everything is setup correctly. I don't know what else to do. I created an issue because I feel like I've done everything correctly, I followed the directions on the gorilla/mux site and I haven't had this issue with other libraries.—
Reply to this email directly or view it on GitHub.
from mux.
I can confirm that using the new route fixed my issue. I got the code by googling "gorilla mux hello world example" and got a really old example.
It is just a case of staring at a problem too long and missing the obvious. I could've figured this out myself if I had just read through mux.go.
@davecheney I read that article and I don't see what's wrong with my GOPATH. It's set to the root of my workspace just like the web page says. Please pm me if you don't feel like replying here. I am eager to learn.
Sorry for creating an issue for something so dumb, and thanks for the help.
from mux.
Related Issues (20)
- [bug] Adding to GET the same endpoint with POST and different Queries ends up with inconsistent error messages
- [question] How to retrieve the handler func without the middleware? HOT 2
- [bug] HOT 17
- Path variable not parsed HOT 2
- [bug] : the link for gorilla mux logo is broken
- [feature] Add CONTRIBUTING.md HOT 4
- [bug]
- [question] Is it true? Is the GWT unarchived, for real? HOT 3
- CORS ERROR HOT 10
- JSON parsing failed HOT 7
- [BUG] Go can't find v2.0 HOT 1
- [BUG] MethodNotAllowedHandler does not work for subrouter with different routes HOT 9
- [BUG] Router does not distinguish between `/` and `%2F` in the request path HOT 1
- Cannot install gorilla/mux with new install command of go HOT 2
- [BUG] Index out of range in (*routeRegexp).Match HOT 1
- [FEATURE] Accidental omission of GetHeaders? HOT 1
- [BUG] API is probably broken for GetQueries per each method HOT 1
- [BUG] runtime error in (*routeRegexp).Match
- [FEATURE] Route metadata
- When I use the subrouter() method, Methods() only works on the last endpoint, and other than the last endpoint, the rest of the endpoints give a 404 instead of a 405 with the unrelated http method. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mux.