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-

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

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络