allenxj
前言:
项目测试需要用到mssqlserver数据库连接,遇到坑,自己爬

直接上代码:
 
 
 
package main

import (
	"database/sql"
	"fmt"
	"strings"


	_ "github.com/denisenkom/go-mssqldb"
)


func main() {
        
	var sqlConnStr=fmt.Println(strings.Join(conf, ";"))

	db, err = sql.Open("mssql", sqlConnStr)
	if err != nil {
		fmt.Println("sql open:", err.Error())
		return
	} else {
		fmt.Println("数据库连接成功...")
	}
        
        ......此处省略
}

 

 
 
运行go文件
----------------------------坑1
命令:go run 3.go

go: cannot determine module path for source directory /Users/XXX/Desktop/Code/Go/src (outside GOPATH, module path must be specified)

Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module

Run 'go help mod init' for more information.


命令:go env 
查看GO111MODULE="on"  是否等于ON
如果不是
就执行
命令:go env -w GO111MODULE=on


----------------------------坑2
命令:go run 3.go

错误:3.go:9:2: no required module provides package github.com/denisenkom/go-mssqldb: go.mod file not found in current directory or any parent directory; see 'go help modules'
问题分析:没有请求到模块提供包github.com/denisenkom/go-mssqldb: go.mod文件在当前目录或任何父目录中找不到

解决方案:
命令:go mod init 3.go
提示:

go: creating new go.mod: module 3.go
go: to add module requirements and sums:
go mod tidy

----------------------------坑3

命令:go run 3.go  (继续运行go文件)
错误:3.go:9:2: no required module provides package github.com/denisenkom/go-mssqldb; to add it:

go get github.com/denisenkom/go-mssqldb

问题分析:没有添加需要引入的包
解决方案:
命令:go get github.com/denisenkom/go-mssqldb
提示:

go get: added github.com/denisenkom/go-mssqldb v0.11.0
go get: added github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe
go get: added golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c

表示倒入包成功

----------------------------

运行go文件
命令:go run 3.go

数据库连接成功......


 

 

分类:

Go

技术点:

相关文章: