donglu9978 2019-04-06 13:51
浏览 315
已采纳

如何用Go编程语言解决数据库结构化问题?

I'm trying to create postgresql database struct to automate the system in Golang.

This code is working;

package main

import (
    "database/sql"
    "fmt"

    _ "github.com/lib/pq"
)

func checkError(err error){
    if err!=nil{
        panic(err)
    }
}

const (
    host     = "localhost"
    port     = 5432
    user     = "postgres"
    password = "123"
    dbname   = "DatabaseName"
)

func main() {
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    checkError(err)

    err = db.Ping()
    checkError(err)

    defer db.Close()
    fmt.Println("Successfully connected!")
}

Result: Successfully connected!

But When I'm trying to create a struct the system giving me an error;

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/lib/pq"
)

func checkError(err error){
    if err!=nil{
        panic(err)
    }
}

type Database struct{
    host, port, user, password, dbname string
    db *sql.DB
}

func(d *Database) Open(){
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"password=%s dbname=%s sslmode=disable", d.host, d.port, d.user, d.password, d.dbname)
    db, err := sql.Open("postgres", psqlInfo)
    checkError(err)
    d.db = db
}

func(d *Database) IsOpened() {
    checkError(d.db.Ping())
}

func main() {
    a := Database{host: "localhost", port: "5432", user: "postgres", password: "123", dbname: "DatabaseName"}
    a.Open()
    a.IsOpened()
}

Result: panic: dial tcp: lookup tcp/%!d(string=5432): getaddrinfow: The specified class could not be found.

goroutine 1 [running]: main.checkError(...) /main.go:11

main.(*Database).IsOpened(...) /main.go:28 main.main() /main.go:34 +0xcc

  • 写回答

1条回答 默认 最新

  • doujie3888 2019-04-07 07:57
    关注

    This happened because you have mashup with data type. You have declared port number as string but when you are building the connection string you have specified port number as integer which will produce host=localhost port=%!d(string=5432) user=postgres password=123 dbname=DatabaseName sslmode=disable wrong connection string. Your connection string should be host=localhost port=5432 user=postgres password=123 dbname=DatabaseName sslmode=disable. You can change the port number data type from string to int in struct

    type Database struct {
        host, user, password, dbname string
        port                         int
        db                           *sql.DB
    }
    

    https://play.golang.org/p/yP3Y-0NdloQ

    Or you can change the port number type from %d to %s when building connection string

    psqlInfo := fmt.Sprintf("host=%s port=%s user=%s "+"password=%s dbname=%s sslmode=disable", d.host, d.port, d.user, d.password, d.dbname)
    

    https://play.golang.org/p/e61dOUg0MA-

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划