ch7/ch7-01 #149
Replies: 14 comments 2 replies
-
练习 7.1 - 有点懵...后面项目实战在理解理解吧// 使用来自ByteCounter的思路,实现一个针对单词和行数的计数器。你会发现bufio.ScanWords非常的有用。
type WordsCount int
type LinesCount int
func (s *WordsCount) Write(p []byte) (n int, err error) {
var sc = bufio.NewScanner(bytes.NewReader(p))
sc.Split(bufio.ScanWords)
for sc.Scan() {
// 由于是值传递, 这里想改变原调用者的值, 必须使用指针
*s++
}
return int(*s), nil
}
func (s *LinesCount) Write(p []byte) (n int, err error) {
var sc = bufio.NewScanner(bytes.NewReader(p))
sc.Split(bufio.ScanLines)
for sc.Scan() {
*s++
}
return int(*s), nil
}
func main() {
var wc WordsCount
wc.Write([]byte("hello world"))
var lc LinesCount
lc.Write([]byte(`hello
1
2
3
world`))
fmt.Println(wc, lc)
fmt.Fprintf(&wc, "Hello, %s", "dugulp")
fmt.Println(wc, lc)
} |
Beta Was this translation helpful? Give feedback.
-
练习7.2 懵逼ing// 封装一个结构体, 此结构体不对外导出
// 内部的两个元素同样是不对外导出的, 只能通过CountingWriter方法获取
type countWriter struct {
w io.Writer
c *int64
}
func (c countWriter) Write(p []byte) (n int, err error) {
var sc = bufio.NewScanner(bytes.NewReader(p))
sc.Split(bufio.ScanWords)
for sc.Scan() {
// 由于是值传递, 这里想改变原调用者的值, 必须使用指针
// 此处n++是要计算新增的单词数
n++
// 此处c++是总的单词数
*c.c++
}
return int(n), nil
}
func CountingWriter(w io.Writer) (io.Writer, *int64) {
var c int64
return &countWriter{w, &c}, &c
} |
Beta Was this translation helpful? Give feedback.
-
练习 7.3type tree struct {
value int
left, right *tree
}
// 实现Stringer接口
func (tree *tree) String() string {
var values []int
values = appendValues(values, tree)
return fmt.Sprint(values)
}
// Build 构建二叉树
func Build(nums []int) *tree {
var root *tree
for _, v := range nums {
root = add(root, v)
}
return root
}
// Sort 排序
func Sort(values []int) {
var root = Build(values)
// 将二叉树还原为切片
appendValues(values[:0], root)
}
func appendValues(values []int, t *tree) []int {
if t != nil {
values = appendValues(values, t.left)
values = append(values, t.value)
values = appendValues(values, t.right)
}
return values
}
// 建树
func add(t *tree, value int) *tree {
if t == nil {
t = new(tree)
t.value = value
return t
}
if value < t.value {
t.left = add(t.left, value)
//fmt.Println("left", t.value)
} else {
t.right = add(t.right, value)
//fmt.Println("right", t.value)
}
//fmt.Println(value)
return t
}
func main() {
var nums []int = []int{4, 5, 43, 323, 45, 98, 4, 5, 7, 8, 1, 3, 2, 565}
var tree = Build(nums)
fmt.Println(tree.String())
} |
Beta Was this translation helpful? Give feedback.
-
7.3package main
import (
"fmt"
)
type tree struct {
value int
left, right *tree
}
// 递归将数组中的数据添加到树中
func add(value int, t *tree) *tree {
if t == nil {
t = &tree{}
t.value = value
return t
}
if t.value > value {
t.left = add(value, t.left)
} else {
t.right = add(value, t.right)
}
return t
}
func sort(values []int) *tree {
var root *tree
for _, val := range values {
root = add(val, root)
}
// 通过这个切片来改变原切片底层数组的值(超过容量时才改变地址)
appendvalues(values[:0], root)
// 返回个值方便测试
return root
}
// 递归把树上的数据添加到数组中
func appendvalues(values []int, t *tree) []int {
if t != nil {
values = appendvalues(values, t.left)
values = append(values, t.value)
values = appendvalues(values, t.right)
}
return values
}
// string 方法
func (t *tree) String() string {
values := appendvalues(make([]int, 0), t)
return fmt.Sprintf("%v", values)
}
func main() {
var m = []int{1, 4, 7, 9, 3, 5, 7, 2, 4}
t := sort(m)
fmt.Println(m)
fmt.Println(t)
} |
Beta Was this translation helpful? Give feedback.
-
总结 Go 语言中的接口是一种抽象类型,它定义了一组方法。任何类型只要实现了接口定义的所有方法,就称之为实现了该接口。接口可以用于定义行为的规范,从而实现代码的解耦和复用。 接口是合约的**意思是指,接口定义了一组方法的规范,就像一份合约一样,规定了某些类型必须遵守的行为。任何类型只要实现了接口定义的所有方法,就相当于签署了这份合约,承诺会遵守约定的行为。 |
Beta Was this translation helpful? Give feedback.
-
练习7.2 懵逼ing |
Beta Was this translation helpful? Give feedback.
-
7.2 func CountingWriter(w io.Writer) (io.Writer, *int64) {
cw := countWriter{w: w}
return &cw, &cw.c
}
type countWriter struct {
w io.Writer
c int64
}
func (cw *countWriter) Write(p []byte) (int, error) {
n, err := cw.w.Write(p)
cw.c += (int64(n))
return n, err
}
func main() {
cw, counter := CountingWriter(os.Stdout)
fmt.Printf("counter before: %d\n", *counter)
fmt.Fprintf(cw, "contentXX\n")
fmt.Printf("counter after: %d\n", *counter)
} |
Beta Was this translation helpful? Give feedback.
-
7.1 练习package struct_method_interface
import "bufio"
type WordRowCounter struct {
lineCount int
wordCount int
}
func (c *WordRowCounter) Write(p []byte) (n int, err error) {
for loc := 0; loc < len(p); {
advance, line, err1 := bufio.ScanLines(p[loc:], true)
if err1 != nil {
return loc, err1
}
c.lineCount++
for loc2 := 0; loc2 < advance-1; {
advance2, _, err2 := bufio.ScanWords(line[loc2:], true)
if err2 != nil {
return loc + loc2, err2
}
c.wordCount++
loc2 += advance2
}
loc += advance
}
return len(p), nil
} |
Beta Was this translation helpful? Give feedback.
-
7.2package struct_method_interface
import "io"
type Counter struct {
w io.Writer
counter *int64
}
func (c *Counter) Write(p []byte) (int, error) {
write, err := c.w.Write(p)
if err != nil {
return 0, err
}
*c.counter = *c.counter + int64(write)
return write, err
}
func CountingWriter(w io.Writer) (io.Writer, *int64) {
counter := new(Counter)
counter.w = w
counter.counter = new(int64)
return counter, counter.counter
} |
Beta Was this translation helpful? Give feedback.
-
7.3以下是使用层序遍历完成的
另外,使用 |
Beta Was this translation helpful? Give feedback.
-
6.1
|
Beta Was this translation helpful? Give feedback.
-
7.2
|
Beta Was this translation helpful? Give feedback.
-
练习 7.3: 为在gopl.io/ch4/treesort(§4.4)中的*tree类型实现一个String方法去展示tree类型的值序列。
|
Beta Was this translation helpful? Give feedback.
-
exercise 7.3 type tree struct {
value int
left, right *tree
}
func (t *tree) String() string {
// inorder
if t == nil {
return ""
}
left := t.left.String()
right := t.right.String()
return fmt.Sprintf("%s%d%s", left, t.value, right)
}
func main() {
root := &tree{
value: 2,
left: &tree{
value: 1,
left: nil,
right: nil,
},
right: &tree{
value: 3,
left: nil,
right: nil,
},
}
fmt.Println(root.String())
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
ch7/ch7-01
中文版
https://golang-china.github.io/gopl-zh/ch7/ch7-01.html
Beta Was this translation helpful? Give feedback.
All reactions