dpgkg42484 2015-12-10 06:43
浏览 87
已采纳

如何跨包中的文件使用全局变量?

I have the following file structure:

models/db.go

type DB struct {
    *sql.DB
}

var db *DB

func init() {
    dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
        DB_USER, DB_PASSWORD, DB_NAME)

    db, err := NewDB(dbinfo)
    checkErr(err)

    rows, err := db.Query("SELECT * FROM profile")
    checkErr(err)

    fmt.Println(rows)
}

func NewDB(dataSourceName string) (*DB, error) {
    db, err := sql.Open("postgres", dataSourceName)
    if err != nil {
        return nil, err
    }
    if err = db.Ping(); err != nil {
        return nil, err
    }
    return &DB{db}, nil
}

models/db_util.go

func (p *Profile) InsertProfile() {
    if db != nil {
        _, err := db.Exec(...)
        checkErr(err)
    } else {
        fmt.Println("DB object is NULL")
    }
}

When I try to access db in InsertProfile function, it says NULL ptr exception. How do I access the db in db_utils.go?

I would not like to capitalize db (as it would give access to all the packages).

I am getting the QUERY returned from the db in init() correctly.

  • 写回答

2条回答 默认 最新

  • dqt83336 2015-12-10 06:45
    关注

    Edit: The problem is that you used Short variable declaration := and you just stored the created *DB value in a local variable and not in the global one.

    This line:

    db, err := NewDB(dbinfo)
    

    Creates 2 local variables: db and err, and this local db has nothing to do with your global db variable. Your global variable will remain nil. You have to assign the created *DB to the global variable. Do not use short variable declaration but simple assignment, e.g:

    var err error
    db, err = NewDB(dbinfo)
    if err != nil {
        log.Fatal(err)
    }
    

    Original answer follows.


    It's a pointer type, you have to initialize it before you use it. The zero value for pointer types is nil.

    You don't have to export it (that's what starting it with a capital letter does). Note that it doesn't matter that you have multiple files as long as they are part of the same package, they can access identifiers defined in one another.

    A good solution would be to do it in the package init() function which is called automatically.

    Note that sql.Open() may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call DB.Ping().

    For example:

    var db *sql.DB
    
    func init() {
        var err error
        db, err = sql.Open("yourdrivername", "somesource")
        if err != nil {
            log.Fatal(err)
        }
        if err = db.Ping(); err != nil {
            log.Fatal(err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路