我在下面的代码中给出了未定义的错误sql.Open
。不确定如何解决此错误。
任何帮助解决此错误?
79:16 sql.Open 未定义(类型字符串没有字段或方法打开)进程退出,代码:2 信号:假
package main
import (
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
_ "github.com/go-sql-driver/mysql"
)
const (
username = ""
password = ""
hostname = ""
dbname = ""
)
type Server struct {
ID int
Servername string
IP sql.NullString `json:"ip"`
OS sql.NullString `json:"os"`
}
func dsn(dbName string) string {
return fmt.Sprintf("%s:%s@tcp(%s)/%s", username, password, hostname, dbName)
}
func RemoveDuplicates(xs *[]string) {
found := make(map[string]bool)
j := 0
for i, x := range *xs {
if !found[x] {
found[x] = true
(*xs)[j] = (*xs)[i]
j++
}
}
*xs = (*xs)[:j]
}
func main() {
var TEMPDIR = os.Getenv("HOME")
if runtime.GOOS == "windows" {
TEMPDIR = "c:\\temp"
}
filename := filepath.FromSlash(TEMPDIR + "/a.txt")
content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
text := string(content)
fmt.Println(text)
w := strings.Fields(text)
RemoveDuplicates(&w)
sort.Strings(w)
j := strings.Join(w, "','")
result := "('" + j + "')"
sql := "SELECT * FROM servers where servername in " + result
fmt.Println("sql: ", sql)
db, err := sql.Open("mysql", dbname)
if err != nil {
log.Printf("Error %s when opening DB\n", err)
return
}
defer db.Close()
log.Printf("Connected to DB\n")
}
谢谢