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 分享给我一个 t00l论坛邀请码
  • ¥15 C# 调用Bartender打印机打印
  • ¥100 华为ensp只要2-9实验运行结果能做的来加我QQ
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 Acrn IVSHMEM doorbell问题
  • ¥15 yolov5中的val测试集训练时数量变小问题