dongzha0813 2018-12-02 02:06
浏览 21
已采纳

新行上的右括号不能编译

This code compiles fine:

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
", r.URL.Path)

    h.ServeHTTP(w, r)
}

but if I add a new-line after the last paren in the fmt.Println call:

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
", r.URL.Path)

    h.ServeHTTP(w, r)
}

now it won't compile, what's the reason for this? I added a comment next to difference in the second code sample, also just writing more b/c it said my question had too much code and not enough words thanks.

  • 写回答

1条回答 默认 最新

  • dqb77047 2018-12-02 02:12
    关注

    The Go Programming Language Specification

    Semicolons

    The formal grammar uses semicolons ";" as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:

    1. When the input is broken into tokens, a semicolon is automatically inserted into the token stream immediately after a line's final token if that token is

      . an identifier

      . an integer, floating-point, imaginary, rune, or string literal

      . one of the keywords break, continue, fallthrough, or return

      . one of the operators and punctuation ++, --, ), ], or }

    2. To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".


    syntax error: unexpected newline, expecting comma or )

    Add the commas at the end of the argument list lines to avoid the automatic insertion of semicolons.

    r.Header.Get("tc_req_body_type"), "expected:", m["request_body"]}, " "),
    ) 
    
    r.Header.Get("tc_req_body_type"), " expected: ", m["request_body"]}, " "),
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序