Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: "instance" redeclared in this block

Tags:

go

I have these two files:

daoFactory.go

package dao

import "sync"

type daoFactory struct {}

var instance *daoFactory

//some functions

fakeProvisionDao.go

package dao

import (
    "sync"
    "model"
)

type provisionDao struct {
}

var instance *provisionDao

//some functions

Both are in the same package: dao.

I get this error:

"instance" redeclared in this block

Obviously, the cause is that instance variable is declared in both files. I'm beggining in Go programming and I don't know how should I handle with this error.

like image 482
Héctor Avatar asked Dec 17 '15 21:12

Héctor


1 Answers

Files have no real meaning for go, unlike in java, python and many others, they are just for you to organize your code as you see fit.

In go variables are visible package wide, that means that both declartions of instance are variables with package wide visibility. Hence the compiler complains about having two global variables with the same name.

Rename any one of your two instance variables and it will compile.

Reading the links in the comments above is highly recommend ;-)

like image 86
tike Avatar answered Sep 27 '22 23:09

tike