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 <https://play.golang.org/p/SzkcLpyJI3>
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
On Wednesday, August 10, 2016 at 9:53:38 AM UTC+2,
[email protected] wrote:
>
> 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.
>
>
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.