8

IDE 是 Visual Studio 代码。这是我打开调试器时的日志:

# _/Users/leon/Documents/Projects/GoLang/TutorialLearningGoForWebDevelopment
/usr/local/go/pkg/tool/darwin_amd64/link: /usr/local/go/pkg/tool/darwin_amd64/link: combining dwarf failed: Unknown load command 0x32 (50)
exit status 2
Process exiting with code: 1

去版本:

go version go1.10.3 darwin/amd64

macOS 版本:

Mojave Version 10.14 Beta (18A353d)

钻研版:

Delve Debugger
Version: 1.0.0
Build: $Id: c98a142125d0b17bb11ec0513bde346229b5f533 $

Visual Studio 代码版本:

  Version 1.26.0-insider (1.26.0-insider)

launch.json几乎是自动生成的,仅更改了端口:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 8080,
            "host": "127.0.0.1",
            "program": "${fileDirname}",
            "env": {},
            "args": [],
            "showLog": true,
            "trace": "verbose"
        }
    ]
}

在这里找到一些类似的问题线程,我没有找到解决方案。
我也安装了go1.11beta3,但我不知道如何在 VSCode 调试方面将其设为默认值。


对症状的更准确描述:
错误不是由Delve侧面引起的,而是源于go程序的第一行:package main

以防万一有人可能需要阅读代码,我将整个文件包括在下面:

package main

import (
    "database/sql"
    "encoding/json"
    "fmt"
    "html/template"
    "io/ioutil"
    "net/http"
    "net/url"

    _ "github.com/mattn/go-sqlite3"

    "encoding/xml"
)

type Page struct {
    Name     string
    DBStatus bool
}

type SearchResult struct {
    Title  string `xml:"title,attr"`
    Author string `xml:"author,attr"`
    Year   string `xml:"hyr,attr"`
    ID     string `xml:"owi,attr"`
}

type ClassifySearchResponse struct {
    Results []SearchResult `xml:"works>work"`
}

type ClassifyBookResponse struct {
    BookData struct {
        Title  string `xml:"title,attr"`
        Author string `xml:"author,attr"`
        ID     string `xml:"owi,attr"`
    } `xml:"work"`
    Classification struct {
        MostPopular string `xml:"sft,attr"`
    } `xml:"recommendations>ddc>mostPopular"`
}

func main() {
    templates := template.Must(template.ParseFiles("templates/index.html"))

    db, _ := sql.Open("sqlite3", "dev_golang.db")

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        p := Page{Name: "Gopher"}
        if name := r.FormValue("name"); name != "" {
            p.Name = name
        }
        p.DBStatus = db.Ping() == nil

        if err := templates.ExecuteTemplate(w, "index.html", p); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
    })

    http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
        var results []SearchResult
        var err error

        if results, err = search(r.FormValue("search")); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }

        encoder := json.NewEncoder(w)
        if err := encoder.Encode(results); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
    })

    http.HandleFunc("/books/add", func(w http.ResponseWriter, r *http.Request) {
        var book ClassifyBookResponse
        var err error

        if book, err = find(r.FormValue("id")); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }

        if err = db.Ping(); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }

        _, err = db.Exec("insert into books (pk, title, author, id, classification) values (?,?,?,?,?)",
            nil,
            book.BookData.Title,
            book.BookData.Author,
            book.BookData.Author,
            book.BookData.ID,
            book.Classification.MostPopular)

        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
    })

    fmt.Println(http.ListenAndServe(":8080", nil))
}

func find(id string) (ClassifyBookResponse, error) {
    var c ClassifyBookResponse
    body, err := classifyAPI("http://classify.oclc.org/classify2/Classify?summary=true&owi=" + url.QueryEscape(id))

    if err != nil {
        return ClassifyBookResponse{}, err
    }

    err = xml.Unmarshal(body, &c)
    return c, err
}

func search(query string) ([]SearchResult, error) {
    var c ClassifySearchResponse
    fmt.Println("query, search(): " + query)
    body, err := classifyAPI("http://classify.oclc.org/classify2/Classify?summary=true&title=" + url.QueryEscape(query))

    if err != nil {
        fmt.Println(err.Error())
        return []SearchResult{}, err
    }

    err = xml.Unmarshal(body, &c)
    fmt.Println(c.Results)
    return c.Results, err
}

func classifyAPI(url string) ([]byte, error) {
    var resp *http.Response
    var err error

    if resp, err = http.Get(url); err != nil {
        return []byte{}, err
    }

    defer resp.Body.Close()

    return ioutil.ReadAll(resp.Body)
}
4

2 回答 2

6

将 GoLang 升级到 go1.11 版本后,我再也没有遇到过这个问题。

于 2018-09-27T15:54:35.313 回答
2

如果您是通过自制软件安装的,请执行以下操作:

brew update
brew upgrade go
于 2018-10-16T18:04:30.573 回答