在 { 之前出现了意外的分号或换行符。

huangapple go评论68阅读模式
英文:

unexpected semicolon or newline before {

问题

我正在为你翻译以下内容:

我正在根据这个视频制作服务器,但代码中出现了以下错误:

在 { 之前出现了意外的分号或换行符
(语法错误 [第10行第1列])

这是 Go 代码:

还有两个 HTML 代码文件,可以在视频中找到,但问题出现在 func main() 中,所以我只发布了 Go 代码。

package main 

import (
    "fmt"
    "log"
    "net/http"
)

func main() 
{
    fileServer := http.FileServer(http.Dir("./static"))
    http.Handle("/", fileServer)
    http.HandleFunc("/form", formHandler)
    http.HandleFunc("/hello", helloHandler)

    fmt.Printf("Starting Server at port 8080\n")

    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal(err)
    }
}

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

    if r.URL.Path != "/hello"{
        http.Error(w, "404 not found", http.StatusNotFound)
        return 
    }

    if r.Method != "GET" {
    http.Error(w, "404 not found", http.StatusNotFound)
    return
    }

    fmt.Fprintf(w, "hello!")
}

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

    if err := r.ParseForm(); err != nil {
        fmt.Fprintf(w, "ParseForm() err: %v", err)
        return
    }

    fmt.Fprintf(w, "POST request successful")
    name := r.FormValue("name")
    address := r.FormValue("address")
    

    fmt.Fprintf(w, "Name = %s\n", name)
    fmt.Fprintf(w, "Address = %s\n", address)

}

我想根据视频制作一个 web 服务器,但是出现了分号语法错误。

英文:

https://youtu.be/ASBUp7stqjo
am making the server from this video and it shows the following error in the code
unexpected semicolon or newline before {
( syntax [Line 10 Col 1] )
here is the golang code

there is also 2 html code files which can be found in the video but the Problem is arising in func main() so i posted only the Go Code

package main 
import (
"fmt"
"log"
"net/http"
)
func main() 
{
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/form", formHandler)
http.HandleFunc("/hello", helloHandler)
fmt.Printf("Starting Server at port 8080\n")
if err := http.ListenAndServe(8080, nil); err != nil {
log.Fatal(err)
}
}
func helloHandler(w http.ResponseWriter, r *http.Request){
if r.URL.Path != "/hello"{
http.Error(w, "404 not found", http.StatusNotFound)
return 
}
if r.Method != "GET" {
http.Error(w, "404 not found", http.StatusNotFound)
return
}
fmt.Fprintf(w, "hello!")
}
func formHandler(w http.ResponseWriter, r *http.Request) {
if err != r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
fmt.Fprintf(w, "POST request successful")
name := r.FormValue("name")
address := r.FormValue("address")
fmt.Fprintf(w, "Name = %s\n", name)
fmt.Fprintf(w, "Address = %s\n", address)
}

i wanted to make a wbe server from the video and it shows a semicolon syntax error

答案1

得分: 1

使用gofmt命令,你可以轻松地识别出任何语法错误。

另一个解决方案是将你的代码粘贴到https://go.dev/play/,然后点击格式化。任何包含语法错误的代码行都会以红色高亮显示。


现在回到你的代码,我看到了两个问题:

第一个问题

func main() 
{

改为

func main() {

第二个问题

if err := http.ListenAndServe(8080, nil); err != nil {

改为

if err := http.ListenAndServe(":8080", nil); err != nil {
英文:

Use gofmt command, with that you will be able to easily identify any syntax error.

Another solution is just to paste your code to https://go.dev/play/, and click format. Any line of code that contains syntax errors will be highlighted in red.


Now back to your code, there are two problems that I can see:

1st problem

Change

func main() 
{

to

func main() {

2nd problem

Change

if err := http.ListenAndServe(8080, nil); err != nil {

to

if err := http.ListenAndServe(":8080", nil); err != nil {

huangapple
  • 本文由 发表于 2023年4月7日 19:25:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75957977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定