RainbowMango / GoExpertProgramming

《Go专家编程》Go语言快速入门,轻松进阶!
1.86k stars 306 forks source link

Example测试疑似bug #61

Open RainbowMango opened 4 years ago

RainbowMango commented 4 years ago

源码:

func RangeString() {
    s := "Hello World"
    for i, v := range s {
        fmt.Printf("index: %d, value: %c\n", i, v)
    }
}

Example:

func ExampleRangeString() {
    RangeString()
    // Output:
    // index: 0, value: H
    // index: 1, value: e
    // index: 2, value: l
    // index: 3, value: l
    // index: 4, value: o
    // index: 5, value:
    // index: 6, value: W
    // index: 7, value: o
    // index: 8, value: r
    // index: 9, value: l
    // index: 10, value: d

结果:

--- FAIL: ExampleRangeString (0.00s)
got:
index: 0, value: H
index: 1, value: e
index: 2, value: l
index: 3, value: l
index: 4, value: o
index: 5, value:
index: 6, value: W
index: 7, value: o
index: 8, value: r
index: 9, value: l
index: 10, value: d
want:
index: 0, value: H
index: 1, value: e
index: 2, value: l
index: 3, value: l
index: 4, value: o
index: 5, value:
index: 6, value: W
index: 7, value: o
index: 8, value: r
index: 9, value: l
index: 10, value: d

如果字符串中包含空格,测试将失败。原因未明。

RainbowMango commented 4 years ago
func StringDoubleQuotationMarks() {
    s := "Hi, \nthis is \"RainbowMango\"."
    fmt.Println(s)
}
func ExampleStringDoubleQuotationMarks() {
    StringDoubleQuotationMarks()
    // Output:
    // Hi,
    // this is "RainbowMango".
}

这个测试也会fail。