summaryrefslogtreecommitdiffhomepage
path: root/go.html.markdown
diff options
context:
space:
mode:
Diffstat (limited to 'go.html.markdown')
-rw-r--r--go.html.markdown56
1 files changed, 54 insertions, 2 deletions
diff --git a/go.html.markdown b/go.html.markdown
index bb6b04eb..0ecc6120 100644
--- a/go.html.markdown
+++ b/go.html.markdown
@@ -116,6 +116,16 @@ can include line breaks.` // Same string type.
learnFlowControl() // Back in the flow.
}
+// It is possible, unlike in many other languages for functions in go
+// to have named return values.
+// Assigning a name to the type being returned in the function declaration line
+// allows us to easily return from multiple points in a function as well as to
+// only use the return keyword, without anything further.
+func learnNamedReturns(x, y int) (z int) {
+ z = x * y
+ return // z is implicit here, because we named it earlier.
+}
+
// Go is fully garbage collected. It has pointers but no pointer arithmetic.
// You can make a mistake with a nil pointer, but not by incrementing a pointer.
func learnMemory() (p, q *int) {
@@ -178,14 +188,42 @@ func learnFlowControl() {
x = 1.3e3 // This makes x == 1300
fmt.Println("xBig:", xBig()) // false now.
+ // What's more is function literals may be defined and called inline,
+ // acting as an argument to function, as long as:
+ // a) function literal is called immediately (),
+ // b) result type matches expected type of argument.
+ fmt.Println("Add + double two numbers: ",
+ func(a, b int) int {
+ return (a + b) * 2
+ }(10, 2)) // Called with args 10 and 2
+ // => Add + double two numbers: 24
+
// When you need it, you'll love it.
goto love
love:
+ learnFunctionFactory() // func returning func is fun(3)(3)
learnDefer() // A quick detour to an important keyword.
learnInterfaces() // Good stuff coming up!
}
+func learnFunctionFactory() {
+ // Next two are equivalent, with second being more practical
+ fmt.Println(sentenceFactory("summer")("A beautiful", "day!"))
+
+ d := sentenceFactory("summer")
+ fmt.Println(d("A beautiful", "day!"))
+ fmt.Println(d("A lazy", "afternoon!"))
+}
+
+// Decorators are common in other languages. Same can be done in Go
+// with function literals that accept arguments.
+func sentenceFactory(mystring string) func(before, after string) string {
+ return func(before, after string) string {
+ return fmt.Sprintf("%s %s %s", before, mystring, after) // new string
+ }
+}
+
func learnDefer() (ok bool) {
// Deferred statements are executed just before the function returns.
defer fmt.Println("deferred statements execute in reverse (LIFO) order.")
@@ -304,17 +342,31 @@ func learnConcurrency() {
// A single function from package http starts a web server.
func learnWebProgramming() {
+
// First parameter of ListenAndServe is TCP address to listen to.
// Second parameter is an interface, specifically http.Handler.
- err := http.ListenAndServe(":8080", pair{})
- fmt.Println(err) // don't ignore errors
+ go func() {
+ err := http.ListenAndServe(":8080", pair{})
+ fmt.Println(err) // don't ignore errors
+ }()
+
+ requestServer();
}
+
// Make pair an http.Handler by implementing its only method, ServeHTTP.
func (p pair) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Serve data with a method of http.ResponseWriter.
w.Write([]byte("You learned Go in Y minutes!"))
}
+
+func requestServer() {
+ resp, err := http.Get("http://localhost:8080")
+ fmt.Println(err)
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ fmt.Printf("\nWebserver said: `%s`", string(body))
+}
```
## Further Reading