Coder Social home page Coder Social logo

Comments (3)

elithrar avatar elithrar commented on May 2, 2024

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.

davecheney avatar davecheney commented on May 2, 2024

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 do

go 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.

luv2code avatar luv2code commented on May 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.