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"]}, " "),
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?