新行上的右括号不编译

7 go

这段代码编译得很好:

return func(w http.ResponseWriter, r *http.Request) {


    if r.Header.Get("tc_req_body_type") != m["request_body"] {
        fmt.Println(
            strings.Join([]string{"types are different", " actual:",
                r.Header.Get("tc_req_body_type"), "expected:", m["request_body"]}," "))
    }

    if r.Header.Get("tc_resp_body_type") != m["response_body"] {
        fmt.Println(
            strings.Join([]string{"types are different", " actual: ",
                r.Header.Get("tc_req_body_type"), " expected: ", m["request_body"]}," "))
    }


    fmt.Printf("Req: %s\n", r.URL.Path)

    h.ServeHTTP(w, r)
}
Run Code Online (Sandbox Code Playgroud)

但是如果我在 fmt.Println 调用中的最后一个括号之后添加一个新行:

return func(w http.ResponseWriter, r *http.Request) {


    if r.Header.Get("tc_req_body_type") != m["request_body"] {
        fmt.Println(
            strings.Join([]string{"types are different", " actual:",
                r.Header.Get("tc_req_body_type"), "expected:", m["request_body"]}," ")
        )  // <<< here
    }

    if r.Header.Get("tc_resp_body_type") != m["response_body"] {
        fmt.Println(
            strings.Join([]string{"types are different", " actual: ",
                r.Header.Get("tc_req_body_type"), " expected: ", m["request_body"]}," ")
        )  // <<<  here
    }


    fmt.Printf("Req: %s\n", r.URL.Path)

    h.ServeHTTP(w, r)
}
Run Code Online (Sandbox Code Playgroud)

现在它不会编译,这是什么原因?我在第二个代码示例中的差异旁边添加了一条评论,也只是写了更多的 b/c,它说我的问题代码太多,字数不够,谢谢。

pet*_*rSO 4

Go 编程语言规范

分号

正式语法使用分号“;” 在多部作品中担任终结者。Go 程序可以使用以下两个规则省略大部分分号:

  1. 当输入被分解为标记时,如果该标记是,则分号会立即自动插入到标记流中,位于该行的最终标记之后

    。一个标识符

    。整数、浮点、虚数、符文或字符串文字

    。关键字 Break、Continue、Fallthrough 或 Return 之一

    。运算符和标点符号之一 ++、--、)、] 或 }

  2. 为了允许复杂的语句占据一行,可以在结束的“)”或“}”之前省略分号。


语法错误:意外的换行符、需要逗号或 )

在参数列表行末尾添加逗号以避免自动插入分号。

r.Header.Get("tc_req_body_type"), "expected:", m["request_body"]}, " "),
) 

r.Header.Get("tc_req_body_type"), " expected: ", m["request_body"]}, " "),
)
Run Code Online (Sandbox Code Playgroud)