【发布时间】:2020-12-19 04:42:25
【问题描述】:
我尝试使用 golang。我是这个 golang 的新手。当我尝试连接到 mysql 时,我遇到了这样的错误:
./testCONnection.go:9:35: 语法错误:意外类型,预期名称 ./testCONnection.go:15:1:语法错误:函数体外的非声明语句 ./testCONnection.go:36:5: 语法错误:函数体外的非声明语句
我已经使用 root 运行了。
这里是我的源代码
主包
import "database/sql"
import "fmt"
import _"github.com/go-sql-driver/mysql"
type trade_history struct {
id, final_meta_report_id, trading_account_id, lp, lp2, lp3 int
symbol, price, price_type, time, type, status, created_at, updated_at string
qty, pegged_distance, price_limit double
}
var db *sql.DB
var err error
funct getTradingHistory (final_Meta_report_ID int) (err error){
username := "lalala"
password := "felixsiong"
host := "trades.xawrs2.us-east-2.rds.amazonaws.com"
port := 3306
db := "trading_dashboard"
//use Sprintf if you wanna parse a string with values
conn := fmt.Sprintf("%s:%s@%s:%s/%s", username, password, host, port, db)
db, err = sql.Open("mysql", conn)
defer db.Close()
if err != nil {
fmt.Println(err.Error())
}
err = db.Ping()
if err != nil {
fmt.Println(err.Error())
}
var p trade_history
//if you have multiple lines of texts use ` instead of "
err = db.QueryRow(`select id, final_meta_report_id,
trading_account_id, symbol, qty, price, price_type, time, lp,
lp2, lp3, pegged_distance, price_limit, time_limit, type, status, created_at`).Scan(&p)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("id: %d\n Final_meta_report_id: %d\n trading_account_id: %d\n symbol: %s\n qty: %.2f\n price: %s\n price_type: %s\n time: %s\n lp: %d\n lp2: %d\n lp3: %d\n pegged_distance: %.2f\n")
return err
}
func main() {
getTradingHistory(2074)
}
我已将我的代码更新为这个
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
var db *sql.DB
var err error
type trade_history struct {
id, final_meta_report_id, trading_account_id, lp, lp2, lp3 int
symbol, price, price_type, time, types, status, created_at, updated_at string
qty, pegged_distance, price_limit float64
}
func getTradingHistory(final_Meta_report_ID int) error {
username := "lalala"
password := "felixsiong"
host := "trades.xawrs2.us-east-2.rds.amazonaws.com"
port := 3306
db := "trading_dashboard"
//use Sprintf if you wanna parse a string with values
conn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", username, password, host, port, db)
//conn := fmt.Sprintf("%s:%s@%s:%s/%s", username, password, host, port, db)
db, err = sql.Open("mysql", conn)
defer db.Close()
if err != nil {
fmt.Println(err.Error())
}
err = db.Ping()
if err != nil {
fmt.Println(err.Error())
}
var p trade_history
//if you have multiple lines of texts use ` instead of "
err := db.QueryRow("select id, final_meta_report_id,
trading_account_id, symbol, qty, price, price_type, time, lp,
lp2, lp3, pegged_distance, price_limit, time_limit, type, status, created_at From trade_history Where final_Meta_report_ID = ?", final_Meta_report_ID ).Scan(&p)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("id: %d\n Final_meta_report_id: %d\n trading_account_id: %d\n symbol: %s\n qty: %.2f\n price: %s\n price_type: %s\n time: %s\n lp: %d\n lp2: %d\n lp3: %d\n pegged_distance: %.2f\n")
return err
}
func main() {
getTradingHistory(2074)
}
但如果我使用终端运行,我仍然会遇到这样的错误
./testCOnnection.go:41:55: newline in string
./testCOnnection.go:41:55: syntax error: unexpected newline, expecting comma or )
./testCOnnection.go:43:57: syntax error: unexpected type at end of statement
./testCOnnec
tion.go:43:109: 无效字符 U+003F '?' ./testCONnection.go:43:144: 字符串中的换行符
如果我使用 Visual Studio 代码运行,我会遇到这样的错误
myConnectionMySql.go:5:8:在以下任何一个中都找不到包“github.com/go-sql-driver/mysql”: /usr/local/go/src/github.com/go-sql-driver/mysql(来自 $GOROOT) /Users/fuad/go/src/github.com/go-sql-driver/mysql(来自 $GOPATH) 退出状态 1
进程退出代码:1我的问题是如何解决我的问题?
【问题讨论】:
-
安装 Jetbrains Goland 或带有 Go 插件的 VSCode 等 IDE 将帮助您编写有效的 Go 代码。甚至在运行代码之前,上面代码中的错误就会被突出显示。
-
你这里的代码很多,想必错误发生在一两行。预计有关 stackoverflow 的一个好问题会将代码减少到仍然重现错误的最小形式。大概是第 41 行附近的代码,你的字符串中有一个换行符。
-
好的,我会第一个检查