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.
The Go Programming Language Specification
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:
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 }
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"]}, " "),
)