-1

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?

Erika
  • 453
  • 8
  • 23

2 Answers2

0

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

Michael
  • 43
  • 1
  • 1
  • 5
  • if I change the main to "dd" for example, I get "Multiple packages in the directory: dd, main". so, in go, I can not use different packages name under the same directory – Erika Sep 02 '22 at 00:37
  • yes, dir is a package in go. you can simply rename one of the 2 variables or create 2 packages – Michael Sep 02 '22 at 19:17
0

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.