首页 > 解决方案 > 使用 Golang 访问数据库时出现架构错误

问题描述

我正在使用 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 安装的,代码中的数据库之前已经在终端命令中创建。

标签: macosgomysql-connector

解决方案


在 GitHub 上查看此问题后,我已解决该问题您所要做的就是在 .zshrc 或 .bash_profile 中添加 Conda 环境设置注释。


推荐阅读