Building a simple web server with Go.

(Notes from Microservices With Go by Nic Richardson)

Building a web server in Go is pretty straightforward, using the HTTP package. We’ll be using net/http package.

Example: basic_server.go

package main

import (
    "fmt"
    "log"
    "net/http"
) 
func main() { 
    port := 8080 

    http.HandleFunc("/helloworld", helloWorldHandler) 
 
    log.Printf("Server starting on port %v\n", 8080) 
    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) 
 } 
 
 func helloWorldHandler(w http.ResponseWriter, r *http.Request) { 
    fmt.Fprint(w, "Hello World\n") 
 }

HandleFunc() on the http package creates a handler, mapping the path in the first parameter to the function in the second parameter.

func HandleFunc(pattern string, handler func(ResponseWriter, *Request)

ListenAndServe() starts an HTTP server with a given address and handler. Here, we are binding it to port 8080. The handler is usually nil, which means to use DefaultServeMux. Handle and HandleFunc add handlers to DefaultServeMux.

If the ListenAndServe function fails to start a server it will return an error, the most common reason for this is that you may be trying to bind to a port that is already in use on the server. In our example, we are passing the output of ListenAndServe straight to log.Fatal(error), which is a convenience function equivalent to calling fmt.Print(a ...interface{}) followed by a call to os.Exit(1). Since ListenAndServe blocks if the server starts correctly we will never exit on a successful start.

$ go run ./basic_server.go  

You should now see the application output:

 Server starting on port 8080  

If you see something like the following, it means you are already running an application on port 8080

 listen tcp :8080: bind: address already in use exit status 1  

You can check the list of running programs using this command

$ ps -aux | grep 'go run'  

If there is another program running, you can kill it. If that is not possible, choose a different port for your program.

Once the server is up and running, type in the URI  http://127.0.0.1:8080/helloworld or http://localhost:8080/helloworld and if things are working correctly you should see the following response from the server:

Hello World 

If you see the above response, that means you have your a basic http server in Go up and running.

How to convert a list to string for display in Python.

(Copied from: https://www.decalage.info/en/python/print_list)

There are a few useful tips to convert a Python list (or any other iterable such as a tuple) to a string for display.

pre { background-color:#F0F0F0; }

First, if it is a list of strings, you may simply use join this way:

>>> mylist = ['spam', 'ham', 'eggs']
>>> print ', '.join(mylist)
spam, ham, eggs

Using the same method, you might also do this:

>>> print '\n'.join(mylist)
spam
ham
eggs

However, this simple method does not work if the list contains non-string objects, such as integers.

If you just want to obtain a comma-separated string, you may use this shortcut:

>>> list_of_ints = [80, 443, 8080, 8081]
>>> print str(list_of_ints).strip('[]')
80, 443, 8080, 8081

Or this one, if your objects contain square brackets:

>>> print str(list_of_ints)[1:-1]
80, 443, 8080, 8081

Finally, you may use map() to convert each item in the list to a string, and then join them:

>>> print ', '.join(map(str, list_of_ints))
80, 443, 8080, 8081
>>> print '\n'.join(map(str, list_of_ints))
80
443
8080
8081
 
 

How to remove stuck entries from Control Panel

Sometimes, even after you uninstall a software, its entry is still available in the Programs and Features section of Control Panel. When you attempt to uninstall it again or remove it from this list, the software complains it cannot be uninstalled or has encountered an error during the uninstall. Such entries can be removed by editing the Registry. Now since this method involves editing/deleting Registry values, please be careful and always create a registry backup before proceeding. Below are the steps:

  1. Click on Start and type ‘regedit’ to open the registry.
  2. Navigate to the following key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

Check for the key ‘Display Name’ on the right side to identify the correct software that you want to remove. If you don’t find your software listed in this location, try looking here:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall

3. Once you find the program you want to delete, right-click on the registry key on the left hand side and click on Delete.

Once deleted from registry, the entry from Programs and Features in Control Panel will be removed.

 

 

Adding Git Auto-completion.

Git Auto-completion is quite useful to auto-complete git commands on the terminal. Here is how to enable this nice script.

1) Using curl, download the script to your home directory:

curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash

2) Make it a dot file:

mv ~/git-completion.bash .git-completion.bash

3) Add this little code snippet to your ~/.bash_profile file:


if [ -f ~/.git-completion.bash ]; then
    source ~/.git-completion.bash
fi

This basically checks if .git-completion.bash exists or not, and if it does, it loads it.4) Quit the terminal window and open it again for the changes to take effect. Alternatively, run

source ~/.bash_profile.

5) To see if this worked correctly, type ‘git h’ and hit tab. It should expand it to ‘git help’.

Reference: Lynda.com Git Tutorials.