Pacakge in golang provides us with powerful tools that can be used to organized code in a simple and elegant way, and we may all know that the same package files must be put under the same directory, but what if the same package name is put under different directories? Let's make an experiment, Here is the illustrated code,
//goproject/src/b/b.go
package b
import (
"fmt"
"goproject/src/c"
)
func init() {
fmt.Println("package b is inited.")
}
func Showb() {
fmt.Println("This is Showb")
}
var Obj c.Student
//goproject/src/d/b.go
package b
import "fmt"
func Showb() {
fmt.Println("This is Showb")
}
//goproject/src/main.go
package main
import (
"goproject/src/b"
b2 "goproject/src/d"
)
type Student struct {
Name string
Id int
}
func (stu *Student) SetName(name string) {
stu.Name = name
}
func main() {
b.Showb()
b2.Showb()
}
So the same package name with b is put under directory b and d respectively, these two packages are in fact not the same, they are two packages, although have the same package name. So the same package should be put under the same dir.