Execute the sqlite3 connector with golang in vscode. Met below error: collect2: error: ld returned 1 exit status

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

Execute the sqlite3 connector with golang in vscode. Met below error: collect2: error: ld returned 1 exit status

问题

package main

import (
	"database/sql"
	"fmt"
	"log"
	"os"

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

type Users struct {
	UserId int
	Uname  string
}

func main() {
	os.Remove("foo.db")

	db, err := sql.Open("sqlite3", "foo.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	sql := `create table users (userId integer, uname text);`
	db.Exec(sql)
	sql = `insert into users(userId,uname) values(1,'Mike');`
	db.Exec(sql)
	sql = `insert into users(userId,uname) values(2,'John');`
	db.Exec(sql)
	rows, err := db.Query("select * from users")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	var users []Users = make([]Users, 0)
	for rows.Next() {
		var u Users
		rows.Scan(&u.UserId, &u.Uname)
		users = append(users, u)
	}
	fmt.Println(users)
}

以上是你的代码。现在我将运行这个文件。代码只是连接到sqlite3数据库,并执行一些SQL语句,没有其他操作。我不知道为什么会出现这个错误。

遇到以下错误:

csccl25013> go run .

github.com/goproject/sqlite3

/nfs/site/disks/simcloud_zhoudo1x_002/go/go/pkg/tool/linux_amd64/link: 运行gcc失败: 退出状态 1
/nfs/site/itools/em64t_SLES12SP5/pkgs/gcc/4.7.2/.bin/../lib64/gcc/x86_64-suse-linux/4.7.2/../../../../x86_64-suse-linux/bin/ld: /tmp/go-link-322571714/000015.o(.text+0x63): 无法解析的 AWAVAUATUSH��h�*H�T$(���� 与符号 `stderr@@GLIBC_2.2.5'
/nfs/site/itools/em64t_SLES12SP5/pkgs/gcc/4.7.2/.bin/../lib64/gcc/x86_64-suse-linux/4.7.2/../../../../x86_64-suse-linux/bin/ld: 最终链接失败: 输出上的不可表示的部分
collect2: 错误: ld 返回 1 退出状态

我不知道如何解决这个错误。请帮助我。

期望的结果:

执行成功。

正常连接到sqlite3。

英文:
package main

import (
	"database/sql"
	"fmt"
	"log"
	"os"

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

type Users struct {
	UserId, intUname string
}

func main() {
	os.Remove("foo.db")

	db, err := sql.Open("sqlite3", "foo.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	sql := `create table users (userId integer, uname text);`
	db.Exec(sql)
	sql = `insert into users(userId,uname) values(1,'Mike');`
	db.Exec(sql)
	sql = `insert into users(userId,uname) values(2,'John');`
	db.Exec(sql)
	rows, err := db.Query("select * from users")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	var users []Users = make([]Users, 0)
	for rows.Next() {
		var u Users
		rows.Scan(&u.UserId, &u.intUname)
		users = append(users, u)
	}
	fmt.Println(users)
}

Above is my codes. Now I will run this file. Codes only connect to sqlite3 database, And execute some sql, No any else operate. I don't know why to happen this.

Met below error:

csccl25013> go run .

github.com/goproject/sqlite3

/nfs/site/disks/simcloud_zhoudo1x_002/go/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/nfs/site/itools/em64t_SLES12SP5/pkgs/gcc/4.7.2/.bin/../lib64/gcc/x86_64-suse-linux/4.7.2/../../../../x86_64-suse-linux/bin/ld: /tmp/go-link-322571714/000015.o(.text+0x63): unresolvable AWAVAUATUSH��h�*H�T$(���� relocation against symbol `stderr@@GLIBC_2.2.5'
/nfs/site/itools/em64t_SLES12SP5/pkgs/gcc/4.7.2/.bin/../lib64/gcc/x86_64-suse-linux/4.7.2/../../../../x86_64-suse-linux/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

I don't know how to resolve this error. Please help me.

Expected Result:

Execute successful.

Connect sqlite3 normally.

答案1

得分: 1

错误提示表明您的Go工具链无法构建github.com/mattn/go-sqlite3的CGO实现(例如,交叉编译错误)。

如果您无法解决C编译器的问题,可以尝试导入modernc.org/sqlite并使用"sqlite" SQL驱动程序。这是一个纯Go版本的Sqlite,避免了对C编译器的需求。

英文:

The error indicates your Go toolchain is unable to build the CGO implementation for github.com/mattn/go-sqlite3 (eg, cross-compilation error).

If you are unable to troubleshoot your C compiler you could try importing modernc.org/sqlite and using the "sqlite" SQL driver instead. This is a pure Go conversion of Sqlite and avoids the need for the C compiler.

huangapple
  • 本文由 发表于 2023年1月4日 16:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75002827.html
匿名

发表评论

匿名网友

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

确定