Home > Software design >  Can not use same variable name in different go file
Can not use same variable name in different go file

Time:09-02

Here is my first go file:

package main

import (
    "bufio"
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "os"
    "strconv"
)

var db13 *sql.DB

then, I create the second go file:

package main

import "database/sql"

var db13 *sql.DB

I got an error saying that: ''db13' redeclared in this package'

Am I miss anything here?

CodePudding user response:

both file are in "package main" so if you think about it like they are in the namespace

CodePudding user response:

They are in same package thus it is not allowed. Also different packages in same directory are not allowed.

db13 declared in first.go can be accessed and used in second.go. No need to declare it again.

  • Related