不同文件上的软件包名称相同

There is same package name in different files in the same folder.

a.go

package abc

type B struct {
}
b.go

package abc

func (b *B) Run() {
}

Can the function defined in the file b.go access a.go without importing anything?

What can be the reasoning behind dividing the code into two different files?

Can the function defined in the file b.go access a.go without importing anything? yes

What can be the reasoning behind dividing the code into two different files? This is just a structuring mechanism, for example when the file would become too large otherwise.

Yes, the Public functions/variables in a.go can be accessed by b.go and vice-versa. The main reason why we put them in different files is to keep all related methods and functionalities together. For eg: In package employee:

  • Combining all use case methods together in a file(let's call its usecase.go)
  • Combining all repository methods together in another file(let's call it repository.go)
  • Combining all constants/enum like constants in another file(let's call it constants.go)

All these are available under the same domain package employee. Each file can access other files public variables/functions. Here usecase.go GetEmployeeData() will access repository.go>GetEmployeeForID(ID int) Similarly, same usecase method GetEmployeeData() will access constants.go constant Employee_Type to decide some logic based on it.

We group things in a file and try making it small and easy to read. Hope this helps.