Add en/0.2.5.md syntax highlighting

This commit is contained in:
vCaesar
2017-04-09 14:56:51 +08:00
parent ac11c27371
commit b6575167b8

View File

@@ -5,25 +5,27 @@ We talked about functions and structs in the last two sections, but did you ever
## method ## method
Suppose you define a "rectangle" struct and you want to calculate its area. We'd typically use the following code to achieve this goal. Suppose you define a "rectangle" struct and you want to calculate its area. We'd typically use the following code to achieve this goal.
```Go
package main
package main import "fmt"
import "fmt"
type Rectangle struct { type Rectangle struct {
width, height float64 width, height float64
} }
func area(r Rectangle) float64 { func area(r Rectangle) float64 {
return r.width*r.height return r.width * r.height
} }
func main() { func main() {
r1 := Rectangle{12, 2} r1 := Rectangle{12, 2}
r2 := Rectangle{9, 4} r2 := Rectangle{9, 4}
fmt.Println("Area of r1 is: ", area(r1)) fmt.Println("Area of r1 is: ", area(r1))
fmt.Println("Area of r2 is: ", area(r2)) fmt.Println("Area of r2 is: ", area(r2))
} }
```
The above example can calculate a rectangle's area. We use the function called `area`, but it's not a method of the rectangle struct (like class methods in classic object-oriented languages). The function and struct are two independent things as you may notice. The above example can calculate a rectangle's area. We use the function called `area`, but it's not a method of the rectangle struct (like class methods in classic object-oriented languages). The function and struct are two independent things as you may notice.
It's not a problem so far. However, if you also have to calculate the area of a circle, square, pentagon, or any other kind of shape, you are going to need to add additional functions with very similar names. It's not a problem so far. However, if you also have to calculate the area of a circle, square, pentagon, or any other kind of shape, you are going to need to add additional functions with very similar names.
@@ -43,45 +45,47 @@ As Rob Pike said.
"A method is a function with an implicit first argument, called a receiver." "A method is a function with an implicit first argument, called a receiver."
Syntax of method. Syntax of method.
```Go
func (r ReceiverType) funcName(parameters) (results) func (r ReceiverType) funcName(parameters) (results)
```
Let's change our example using `method` instead. Let's change our example using `method` instead.
```Go
package main
package main import (
import ( "fmt"
"fmt" "math"
"math" )
)
type Rectangle struct { type Rectangle struct {
width, height float64 width, height float64
} }
type Circle struct { type Circle struct {
radius float64 radius float64
} }
func (r Rectangle) area() float64 { func (r Rectangle) area() float64 {
return r.width*r.height return r.width * r.height
} }
func (c Circle) area() float64 { func (c Circle) area() float64 {
return c.radius * c.radius * math.Pi return c.radius * c.radius * math.Pi
} }
func main() { func main() {
r1 := Rectangle{12, 2} r1 := Rectangle{12, 2}
r2 := Rectangle{9, 4} r2 := Rectangle{9, 4}
c1 := Circle{10} c1 := Circle{10}
c2 := Circle{25} c2 := Circle{25}
fmt.Println("Area of r1 is: ", r1.area()) fmt.Println("Area of r1 is: ", r1.area())
fmt.Println("Area of r2 is: ", r2.area()) fmt.Println("Area of r2 is: ", r2.area())
fmt.Println("Area of c1 is: ", c1.area()) fmt.Println("Area of c1 is: ", c1.area())
fmt.Println("Area of c2 is: ", c2.area()) fmt.Println("Area of c2 is: ", c2.area())
} }
```
Notes for using methods. Notes for using methods.
- If the name of methods are the same but they don't share the same receivers, they are not the same. - If the name of methods are the same but they don't share the same receivers, they are not the same.
@@ -99,103 +103,105 @@ One thing that's worth noting is that the method with a dotted line means the re
Can the receiver only be a struct? Of course not. Any type can be the receiver of a method. You may be confused about customized types. Struct is a special kind of customized type -there are more customized types. Can the receiver only be a struct? Of course not. Any type can be the receiver of a method. You may be confused about customized types. Struct is a special kind of customized type -there are more customized types.
Use the following format to define a customized type. Use the following format to define a customized type.
```Go
type typeName typeLiteral type typeName typeLiteral
```
Examples of customized types: Examples of customized types:
```Go
type ages int
type ages int type money float32
type money float32 type months map[string]int
type months map[string]int m := months {
"January":31,
m := months { "February":28,
"January":31, ...
"February":28, "December":31,
... }
"December":31, ```
}
I hope that you know how to use customized types now. Similar to `typedef` in C, we use `ages` to substitute `int` in the above example. I hope that you know how to use customized types now. Similar to `typedef` in C, we use `ages` to substitute `int` in the above example.
Let's get back to talking about `method`. Let's get back to talking about `method`.
You can use as many methods in custom types as you want. You can use as many methods in custom types as you want.
```Go
package main
package main import "fmt"
import "fmt"
const( const (
WHITE = iota WHITE = iota
BLACK BLACK
BLUE BLUE
RED RED
YELLOW YELLOW
) )
type Color byte type Color byte
type Box struct { type Box struct {
width, height, depth float64 width, height, depth float64
color Color color Color
}
type BoxList []Box //a slice of boxes
func (b Box) Volume() float64 {
return b.width * b.height * b.depth
}
func (b *Box) SetColor(c Color) {
b.color = c
}
func (bl BoxList) BiggestsColor() Color {
v := 0.00
k := Color(WHITE)
for _, b := range bl {
if b.Volume() > v {
v = b.Volume()
k = b.color
}
}
return k
}
func (bl BoxList) PaintItBlack() {
for i, _ := range bl {
bl[i].SetColor(BLACK)
}
}
func (c Color) String() string {
strings := []string{"WHITE", "BLACK", "BLUE", "RED", "YELLOW"}
return strings[c]
}
func main() {
boxes := BoxList{
Box{4, 4, 4, RED},
Box{10, 10, 1, YELLOW},
Box{1, 1, 20, BLACK},
Box{10, 10, 1, BLUE},
Box{10, 30, 1, WHITE},
Box{20, 20, 20, YELLOW},
} }
type BoxList []Box //a slice of boxes fmt.Printf("We have %d boxes in our set\n", len(boxes))
fmt.Println("The volume of the first one is", boxes[0].Volume(), "cm³")
fmt.Println("The color of the last one is", boxes[len(boxes)-1].color.String())
fmt.Println("The biggest one is", boxes.BiggestsColor().String())
func (b Box) Volume() float64 { fmt.Println("Let's paint them all black")
return b.width * b.height * b.depth boxes.PaintItBlack()
} fmt.Println("The color of the second one is", boxes[1].color.String())
func (b *Box) SetColor(c Color) { fmt.Println("Obviously, now, the biggest one is", boxes.BiggestsColor().String())
b.color = c }
}
func (bl BoxList) BiggestsColor() Color { ```
v := 0.00
k := Color(WHITE)
for _, b := range bl {
if b.Volume() > v {
v = b.Volume()
k = b.color
}
}
return k
}
func (bl BoxList) PaintItBlack() {
for i, _ := range bl {
bl[i].SetColor(BLACK)
}
}
func (c Color) String() string {
strings := []string {"WHITE", "BLACK", "BLUE", "RED", "YELLOW"}
return strings[c]
}
func main() {
boxes := BoxList {
Box{4, 4, 4, RED},
Box{10, 10, 1, YELLOW},
Box{1, 1, 20, BLACK},
Box{10, 10, 1, BLUE},
Box{10, 30, 1, WHITE},
Box{20, 20, 20, YELLOW},
}
fmt.Printf("We have %d boxes in our set\n", len(boxes))
fmt.Println("The volume of the first one is", boxes[0].Volume(), "cm³")
fmt.Println("The color of the last one is",boxes[len(boxes)-1].color.String())
fmt.Println("The biggest one is", boxes.BiggestsColor().String())
fmt.Println("Let's paint them all black")
boxes.PaintItBlack()
fmt.Println("The color of the second one is", boxes[1].color.String())
fmt.Println("Obviously, now, the biggest one is", boxes.BiggestsColor().String())
}
We define some constants and customized types. We define some constants and customized types.
- Use `Color` as alias of `byte`. - Use `Color` as alias of `byte`.
@@ -225,79 +231,83 @@ You may also be asking whether we should use `(&bl[i]).SetColor(BLACK)` in `Pain
### Inheritance of method ### Inheritance of method
We learned about inheritance of fields in the last section. Similarly, we also have method inheritance in Go. If an anonymous field has methods, then the struct that contains the field will have all the methods from it as well. We learned about inheritance of fields in the last section. Similarly, we also have method inheritance in Go. If an anonymous field has methods, then the struct that contains the field will have all the methods from it as well.
```Go
package main
package main import "fmt"
import "fmt"
type Human struct { type Human struct {
name string name string
age int age int
phone string phone string
} }
type Student struct { type Student struct {
Human // anonymous field Human // anonymous field
school string school string
} }
type Employee struct { type Employee struct {
Human Human
company string company string
} }
// define a method in Human // define a method in Human
func (h *Human) SayHi() { func (h *Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
} }
func main() { func main() {
mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"} mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"}
sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"} sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"}
mark.SayHi() mark.SayHi()
sam.SayHi() sam.SayHi()
} }
```
### Method overload ### Method overload
If we want Employee to have its own method `SayHi`, we can define a method that has the same name in Employee, and it will hide `SayHi` in Human when we call it. If we want Employee to have its own method `SayHi`, we can define a method that has the same name in Employee, and it will hide `SayHi` in Human when we call it.
```Go
package main
package main import "fmt"
import "fmt"
type Human struct { type Human struct {
name string name string
age int age int
phone string phone string
} }
type Student struct { type Student struct {
Human Human
school string school string
} }
type Employee struct { type Employee struct {
Human Human
company string company string
} }
func (h *Human) SayHi() { func (h *Human) SayHi() {
fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
} }
func (e *Employee) SayHi() { func (e *Employee) SayHi() {
fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,
e.company, e.phone) //Yes you can split into 2 lines here. e.company, e.phone) //Yes you can split into 2 lines here.
} }
func main() { func main() {
mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"} mark := Student{Human{"Mark", 25, "222-222-YYYY"}, "MIT"}
sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"} sam := Employee{Human{"Sam", 45, "111-888-XXXX"}, "Golang Inc"}
mark.SayHi() mark.SayHi()
sam.SayHi() sam.SayHi()
} }
```
You are able to write an Object-oriented program now, and methods use rule of capital letter to decide whether public or private as well. You are able to write an Object-oriented program now, and methods use rule of capital letter to decide whether public or private as well.
## Links ## Links