Small complete examples which show the power of Go?

468 views
Skip to first unread message

gary.wi...@victoriaplumb.com

unread,
Aug 10, 2016, 3:53:38 AM8/10/16
to golang-nuts
Hi,

I'm giving a talk at work introducing Go and I'm looking for small examples to show Go's potential. For example, the following program demonstrates a bare-bones webserver in 13 lines:

import (

   
"fmt"
   
"net/http"
)
 
func home
(w http.ResponseWriter, r *http.Request) {
    fmt
.Fprintf(w, "Hello, world!")
}
 
func main
() {
    http
.HandleFunc("/", home)
    http
.ListenAndServe(":8080", nil)
}

Has anyone here got any more little snippets like this to show the power and potential of Go? It doesn't have to be in networking, just little a little snippet to make people think "wow, that's cool!".

Thanks.


Anmol Sethi

unread,
Aug 10, 2016, 5:09:58 AM8/10/16
to gary.wi...@victoriaplumb.com, golang-nuts
You can shorten that by 6 lines

package main

import "net/http"

func main() {
http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!"))
}))
}

A bare bones web server in just 9 lines!
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

sphil...@gmail.com

unread,
Aug 10, 2016, 5:43:12 AM8/10/16
to golang-nuts, gary.wi...@victoriaplumb.com
Easy parallelization and concurrency:

package main

import (
     
"runtime"
)
 
func
FoldParallel(data []float64, initialValue float64, op func(float64, float64) float64) float64 {
    sliceSize
:= len(data) / runtime.NumCPU()
    results
:= make(chan float64, runtime.NumCPU())
    numResults
:= 0
   
for i := 0; i < len(data); i += sliceSize {
        numResults
++
        go func
(dataSlice []float64) {
            result
:= float64(initialValue)
           
for _, v := range dataSlice {
                op
(result, v)
           
}
            results
<- result
       
}(data[i : i+sliceSize])
   
}
    result
:= initialValue
   
for i := 0; i < numResults; i++ {
        result
= op(result, <-results)
   
}
   
return result
}

func main
() {
   
var data [1000]float64

   
// parallel sum
   
FoldParallel(data[:], 0, func(v1, v2 float64) float64 { return v1 + v2 })
   
// parallel mul
   
FoldParallel(data[:], 1, func(v1, v2 float64) float64 { return v1 * v2 })
}


Val

unread,
Aug 10, 2016, 6:28:39 AM8/10/16
to golang-nuts, gary.wi...@victoriaplumb.com
Iterations over slices, maps, channels are very cool, usually straight to the point :

func main() {
    for _, a := range []int{6, 4} {
        for _, b := range []int{2, 3} {
            for fname, f := range map[string]func(int, int) int{
                "plus":  func(x, y int) int { return x + y },
                "minus": func(x, y int) int { return x - y },
                "times": func(x, y int) int { return x * y },
                "div":   func(x, y int) int { return x / y },
            } {
                fmt.Println(a, fname, b, "is", f(a, b))
            }
        }
    }
}
Playground

Then you may tell some specific details : why the underscores for ignored iteration variables, and why the map iteration order is not the same as the order in the code. Also, I find these iterations quite versatile in practice, but they work only on built-in types (you won't have java-like custom iterables).

Cheers
Val

Paul Rosenzweig

unread,
Aug 10, 2016, 1:06:10 PM8/10/16
to gary.wi...@victoriaplumb.com, golang-nuts
I always liked Rob Pike's concurrent prime seive: https://play.golang.org/p/9U22NfrXeq

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

Rob Pike

unread,
Aug 10, 2016, 3:57:11 PM8/10/16
to Paul Rosenzweig, gary.wi...@victoriaplumb.com, golang-nuts
It's not really mine. Tom Cargill first showed it to me, but it started with Doug McIlroy and I think it originates from an idea by David Gries.

-rob

On Thu, Aug 11, 2016 at 2:27 AM, Paul Rosenzweig <paularo...@gmail.com> wrote:
I always liked Rob Pike's concurrent prime seive: https://play.golang.org/p/9U22NfrXeq

Rob Pike

unread,
Aug 10, 2016, 3:58:28 PM8/10/16
to Paul Rosenzweig, gary.wi...@victoriaplumb.com, golang-nuts
P.S. The version I first saw was of course not in Go, it was in Newsqueak, but the first complete program ever written in Go (not first executed) was the prime sieve in the first draft of the language specification.

-rob

as....@gmail.com

unread,
Aug 13, 2016, 3:02:52 PM8/13/16
to golang-nuts, gary.wi...@victoriaplumb.com
It's shorter, but it's harder for me to grasp what it does as easily as the first example.

anupam...@gmail.com

unread,
Aug 16, 2016, 12:15:14 AM8/16/16
to golan...@googlegroups.com

,----
| I always liked Rob Pike's concurrent prime seive:
| https://play.golang.org/p/9U22NfrXeq
`----
i think what you are looking for is an elegant paper by doug-mcllroy
called "squinting at power series"

which bring the power of channels etc to stream processing...

--
kind regards
anupam

Reply all
Reply to author
Forward
0 new messages