Jacob Kranz
Jacob Kranz

Reputation: 951

how to have Go accept web requests

I don't have the exact terminology so stay with me.

For php when a request comes in, say to http://api.example.com/users/42, Apache redirects the request to the appropriate directory.

In Go, how would I capture the http://api.example.com/users/42 and then serve the output, such as JSON? Would I use the net package and listen on port 80?

I'm sure this is pretty elementary, but I don't think I have the correct terminology hence why it's a little hard to look up.

Upvotes: 1

Views: 104

Answers (1)

OneOfOne
OneOfOne

Reputation: 99361

I highly recommend reading the Wiki, specially this article, also check this excelent book : Build Web Application with Golang

basic idea is :

package main

func main() {
    http.HandleFunc("/users/", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello"))
    })
    http.ListenAndServe(":80", nil)
}

Note that to listen on port 80 you have to be root.

Upvotes: 2

Related Questions