Search code examples
macosgogodoc

Automatically start godoc localhost server in OS X?


In Go, you can start HTTP server and then browse through the Go document via the specific port. For example, if you type in godoc -http=:3333 in Terminal, the localhost server starts working on port 3333 and you can then view the official Go document.

However, I would like to make it start automatically whenever I log in to the OS X system, since it is so powerful and convenient to write in Go code with even when I'm off the Wi-Fi connection. So is it feasible to use such daemon in OS X?

I have implemented and utilized the exact functionality in MongoDB from an example here, and it's exactly this kind of service that I want to achieve...


Solution

  • Put this in the folder ~/Library/LaunchAgents with a file name like org.golang.doc.example.plist:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN
               http://www.apple.com/DTDs/PropertyList-1.0.dtd >
    <plist version="1.0">
      <dict>
        <key>Label</key>
        <string>org.golang.doc.example</string>
        <key>ProgramArguments</key>
        <array>
          <string>/usr/local/go/bin/godoc</string>
          <string>-http=:6060</string>
        </array>
        <key>KeepAlive</key>
        <true />
      </dict>
    </plist>
    

    You may have to create the folder yourself.

    The next time you log into your account, godoc should automatically start on port 6060.

    (I have not tested it very well though. Feedback welcome!)