Home > Article > Backend Development > How Can I Organize My Go Projects with Subfolders Using Go Modules?
In your project setup, you encounter an error when importing the user.go file into the main.go file. This occurs because the User type is not defined in the main package.
To resolve this issue and effectively organize your project into subfolders, we recommend leveraging Go modules. This feature was introduced in Go v1.11.1 and allows for project organization similar to namespaces and subdirectories.
Consider the following project structure:
├── main.go └── src └── models └── user.go └── go.mod
main.go
package main import "my-module/src/models/user" func main() { fmt.Println(user.User{"new_user"}) }
user.go
package user type User struct { Login string }
go.mod
module my-module
By leveraging Go modules, you can organize your project into subfolders, reflecting a namespace-like structure. Packages can be imported using module paths, providing a clean and efficient organization for your codebase.
The above is the detailed content of How Can I Organize My Go Projects with Subfolders Using Go Modules?. For more information, please follow other related articles on the PHP Chinese website!