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-

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

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料