'Go Nested Folders and Referencing Types/Structs from Parent Folders
Silly Question but I am hoping someone can simply clarify what I am doing wrong...
I have Two Structs:
type Client struct{
ConnectionDetails *Connection
}
type Connection struct{
ClientObj *Client
URL string
Name string
}
I would like to store these in a nested folder structure that is similar to the nesting of the struct. For Example
| ---> [Root]project
| -----> main.go
| -----> types.go
| -----> [Folder]connection/
| ---------> types.go
| ---------> connections.go
from main.go I can import "project/connection" and thats no problem. However i still get the error saying that in connections.go Client is undefined.
I know this is because inside connections.go it is not aware of the struct declared within the parent folder "project/types.go".
How does one overcome this issue where you are in a submodule and need to reference a type that is declared in a parent module.
I need to understand this, hopefully with a couple of examples as I am intending to nest multiple folder levels deep in my application.
For Example
Given this folder structure. /project/client/api/v1/items/item/action
In the item folder there would be a struct called "item" and that "item struct" might contain an object called "client" which is a pointer to *Client.
type item struct{
Name string
Data string
Client *Client
}
But this Client Struct was declared in the types.go file that exists in the Client folder...
How does this work without having to declare struct Client in every subfolder of the whole chain and maintaining it in multiple locations.
Thanks so much for your help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|