OSDN Git Service

Regular updates
[twpd/master.git] / go.md
diff --git a/go.md b/go.md
index 581521d..c122862 100644 (file)
--- a/go.md
+++ b/go.md
@@ -184,13 +184,13 @@ See: [If](https://tour.golang.org/flowcontrol/5)
 ### Statements in if
 
 ```go
-if _, err := getResult(); err != nil {
+if _, err := doThing(); err != nil {
   fmt.Println("Uh oh")
 }
 ```
 {: data-line="1"}
 
-A condition in an `if` statement can be preceded with a statement before a `;`.
+A condition in an `if` statement can be preceded with a statement before a `;`. Variables declared by the statement are only in scope until the end of the `if`.
 
 See: [If with a short statement](https://tour.golang.org/flowcontrol/6)
 
@@ -233,6 +233,18 @@ for i, val := range entry {
 
 See: [For-Range loops](https://gobyexample.com/range)
 
+### While loop
+
+```go
+n := 0
+x := 42
+for n != x {
+  n := guess()
+}
+```
+
+See: [Go's "while"](https://tour.golang.org/flowcontrol/3)
+
 ## Functions
 {: .-three-column}
 
@@ -581,6 +593,50 @@ By defining your receiver as a pointer (`*Vertex`), you can do mutations.
 
 See: [Pointer receivers](https://tour.golang.org/methods/4)
 
+## Interfaces
+
+### A basic interface
+
+```go
+type Shape interface {
+  Area() float64
+  Perimeter() float64
+}
+```
+
+### Struct
+
+```go
+type Rectangle struct {
+  Length, Width float64
+}
+```
+
+Struct `Rectangle` implicitly implements interface `Shape` by implementing all of its methods.
+
+### Methods
+
+```go
+func (r Rectangle) Area() float64 {
+  return r.Length * r.Width
+}
+
+func (r Rectangle) Perimeter() float64 {
+  return 2 * (r.Length + r.Width)
+}
+```
+
+The methods defined in `Shape` are implemented in `Rectangle`.
+
+### Interface example
+
+```go
+func main() {
+  var r Shape = Rectangle{Length: 3, Width: 4}
+  fmt.Printf("Type of r: %T, Area: %v, Perimeter: %v.", r, r.Area(), r.Perimeter())
+}
+```
+
 ## References
 
 ### Official resources