News for hackers. Stuff that matters.
Re:"100% of our production system is now running Go" by tocomment on Friday August 31, 2012 @08:42AM (#4459381)
How do you run golang as a server? Can anyone point me to a basic tutorial? Is it still running under something like Apache is golang IS the server somehow? If so Wouldn't each http request have to fire up the compiled executable? Isn't that slow?
Re:"100% of our production system is now running Go" by billwashere on Friday August 31, 2012 @09:03AM (#4459474)
>>How do you run golang as a server?

Using the net/http package that comes with Go.

>>Can anyone point me to a basic tutorial?

To learn Go - http://tour.golang.org/

To learn about http http://golang.org/doc/effective_go.html (A list of available functions are found here http://golang.org/pkg/net/http/)

Gorilla is a great web framework that builds upon the net/http package - http://gorilla-web.appspot.com/

>>Is it still running under something like Apache is golang IS the server somehow? If so Wouldn't each http request have to fire up the compiled executable?

What you are describing is CGI (http://en.wikipedia.org/wiki/Common_Gateway_Interface) whilst it could be possible but don't for real severs its really old and slow.

Re:"100% of our production system is now running Go" by eckyptang on Friday August 31, 2012 @09:18AM (#4459532)
The compiled output will contain your network server. you run the process and it listens on a socket. It is pretty straightforward. Using HTTP as an example, each request will run in a goroutine which is somewhere between a thread and a coroutine.

It is extremely fast.

The main risk is process failure which is very rare but can be mitigated via putting a front end balancer such as nginx or apache mod_proxy. Process recovery and spawning can be managed by another tool such as supervisord.

However, you can run a process happily from init if you desire.

Re:"100% of our production system is now running Go" by tocomment on Friday August 31, 2012 @09:48AM (#4459661)
I was always taught never to write my own web server since something like Apache has been tested on millions of sites, and you have security experts designing it.

Does writing your own server in Go kind of violate this idea? Are you opening yourself up to re-inventing a web server and repeating decades of mistakes something like Apache has already figured out and fixed?

Re:"100% of our production system is now running Go" by eckyptang on Friday August 31, 2012 @09:56AM (#4459692)
You are correct - you should never write your own web server. In this case, you're not writing your own web server. There is one provided with the standard library which will be embedded into your binary.

You are writing a bunch of handlers (as you would with Apache) and plugging them into the compiled in web server from the standard library.

For network servers, there is standard library support for SMTP, HTTP, normal stream based text protocols etc as well as serialization formatters and parsers for JSON, XML, HTML etc. You should never have to re-invent the wheel.

Re:"100% of our production system is now running Go" by tptacek on Friday August 31, 2012 @11:41AM (#4460148)
That's why you front-end app servers with reverse proxies like nginx and Varnish. In practical reality, most major sites run applications out of app servers that are less well tested than Apache. This is not a real objection.
Re:"100% of our production system is now running Go" by exch on Friday August 31, 2012 @10:04AM (#4459729)
Writing a (web)server in Go is not so much about reinventing the wheel, as it is about slapping together the existing building blocks into a server specifically tailored to your application.

These building blocks are very complete and are all available in the standard library. This means they are tested, debugged, tested again and have become pretty damn robust.

The point about Apache/NGinx being well established and battle hardened still stands, but Go's library is quickly following suit.

What Go offers over the others in this respect, is that you do not have to settle for a monolithic one-size-fits-all package. The code you write in Go does one thing (serve your application) and does it well without any compromises.

Re:"100% of our production system is now running Go" by ehutch79 on Friday August 31, 2012 @10:36AM (#4459883)
It should also be noted, that you're not being encourage to serve static media with your go app. it's more like fastcgi or wsgi. you would have apache or nginx sitting in front of your app listening only on localhost serving dynamic pages only.
Re:"100% of our production system is now running Go" by tocomment on Friday August 31, 2012 @09:46AM (#4459655)
Thanks! That's exactly what I was trying to figure out. You're a gem.

So is there anywhere I can see an example of code that runs a server like this?

Re:"100% of our production system is now running Go" by eckyptang on Friday August 31, 2012 @09:51AM (#4459674)
From the tour - I've added comments for clarity:

   // this defines the entry point
   package main
   
   // import formatting and HTTP server packages
   import (
   	"fmt"
   	"net/http"
   )

   // This is your HTTP Server instance
   type Hello struct{}
   
   // This is a method on your HTTP server instance that sends hello for all requests
   func (h Hello) ServeHTTP(
   		w http.ResponseWriter,
   		r *http.Request) {
   	fmt.Fprint(w, "Hello!")
   }
   
   // this is your entry point
   func main() {
        // create a new HTTP Server instance
   	var h Hello
        // tell the runtime to serve it
   	http.ListenAndServe("localhost:4000",h)
   }