macos - 使用Golang访问数据库时出现架构错误

标签 macos go mysql-connector

我正在使用Golang并尝试通过以下代码连接MySQL,这也是go-database\sql上显示的教程代码。但是,报告某些文件不是针对体系结构x86_64构建的,并且找不到针对体系结构x86_64的某些符号时,出现了一些错误。
所有代码均在macOS 10.15.4平台上的Goland(2019.3.4)中进行了编辑。

package main

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "log"
)

func main() {
    db, err := sql.Open("mysql", "root:mmmm572011@tcp(127.0.0.1:3306)/bookstore")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
}

这是我得到的:
GOROOT=/usr/local/Cellar/go/1.14/libexec #gosetup
GOPATH=/Users/michaeltan/go #gosetup
/usr/local/Cellar/go/1.14/libexec/bin/go build -o /private/var/folders/tr/c6mwrj1928949n_6pnpj3mk40000gn/T/___go_build_main_go /Users/michaeltan/GolandProjects/test_sql/main.go #gosetup
# runtime/cgo
ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libpthread.tbd, file was built for unsupported file format ( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) which is not the architecture being linked (x86_64): /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libpthread.tbd
ld: warning: ignoring file /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, file was built for unsupported file format ( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) which is not the architecture being linked (x86_64): /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd
Undefined symbols for architecture x86_64:
  "___stack_chk_fail", referenced from:
      _x_cgo_init in _x004.o
      __cgo_sys_thread_start in _x004.o
  "___stack_chk_guard", referenced from:
      _x_cgo_init in _x004.o
      __cgo_sys_thread_start in _x004.o
  "___stderrp", referenced from:
      __cgo_sys_thread_start in _x004.o
      _x_cgo_sys_thread_create in _x005.o
      _x_cgo_thread_start in _x008.o
  "_abort", referenced from:
      __cgo_sys_thread_start in _x004.o
      _x_cgo_sys_thread_create in _x005.o
      _x_cgo_thread_start in _x008.o
  "_fprintf", referenced from:
      __cgo_sys_thread_start in _x004.o
      _x_cgo_sys_thread_create in _x005.o
  "_free", referenced from:
      _threadentry in _x004.o
  "_fwrite", referenced from:
      _x_cgo_thread_start in _x008.o
  "_malloc", referenced from:
      _x_cgo_thread_start in _x008.o
  "_nanosleep", referenced from:
      _x_cgo_sys_thread_create in _x005.o
      __cgo_try_pthread_create in _x005.o
  "_pthread_attr_destroy", referenced from:
      _x_cgo_init in _x004.o
  "_pthread_attr_getstacksize", referenced from:
      _x_cgo_init in _x004.o
      __cgo_sys_thread_start in _x004.o
  "_pthread_attr_init", referenced from:
      _x_cgo_init in _x004.o
      __cgo_sys_thread_start in _x004.o
  "_pthread_cond_broadcast", referenced from:
      _x_cgo_notify_runtime_init_done in _x005.o
  "_pthread_cond_wait", referenced from:
      __cgo_wait_runtime_init_done in _x005.o
  "_pthread_create", referenced from:
      _x_cgo_sys_thread_create in _x005.o
      __cgo_try_pthread_create in _x005.o
     (maybe you meant: __cgo_try_pthread_create)
  "_pthread_detach", referenced from:
      _x_cgo_sys_thread_create in _x005.o
      __cgo_try_pthread_create in _x005.o
  "_pthread_mutex_lock", referenced from:
      __cgo_wait_runtime_init_done in _x005.o
      _x_cgo_notify_runtime_init_done in _x005.o
      _x_cgo_set_context_function in _x005.o
      __cgo_get_context_function in _x005.o
  "_pthread_mutex_unlock", referenced from:
      __cgo_wait_runtime_init_done in _x005.o
      _x_cgo_notify_runtime_init_done in _x005.o
      _x_cgo_set_context_function in _x005.o
      __cgo_get_context_function in _x005.o
  "_pthread_sigmask", referenced from:
      __cgo_sys_thread_start in _x004.o
  "_setenv", referenced from:
      _x_cgo_setenv in _x006.o
     (maybe you meant: _x_cgo_setenv)
  "_strerror", referenced from:
      __cgo_sys_thread_start in _x004.o
      _x_cgo_sys_thread_create in _x005.o
  "_unsetenv", referenced from:
      _x_cgo_unsetenv in _x006.o
     (maybe you meant: _x_cgo_unsetenv)
ld: symbol(s) not found for architecture x86_64
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)

Compilation finished with exit code 2

附加信息:
Golang(1.14 Darwin / amd64)是通过Homebrew安装的。
Mysql是通过Homebrew安装的,并且在终端命令中以前已经在代码中创建了数据库。

最佳答案

您是否真的需要通过Brew安装Go?我不信任此安装。我更喜欢使用官方安装来避免问题。
https://dl.google.com/go/go1.14.1.darwin-amd64.pkg

关于macos - 使用Golang访问数据库时出现架构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60887264/

相关文章:

swift - 添加和增加文本字段输入的数字

java - 如何在 Eclipse 中使用 MySql 数据库

c# - 通过选择 UserID(PK) 在 ExecuteReader 上出现 MySqlException

关闭 Go channel 时的​​ Go 竞争条件

Python-属性错误: module 'mysql' has no attribute 'connector'

c++ - MacOS 上的 gcc : Strange threading error

c++ - 当一个单独的应用程序写入日志文件时从日志文件中读取数据

macos - Cocoa可编辑文本,无输入框

循环中的 Golang goroutines

go - 如何使用 Artifactory 作为 Go 模块的本地存储库