How to Import Local Package in Golang

huangapple go评论120阅读模式
英文:

How to Import Local Package in Golang

问题

我有一个问题。我无法在我的应用程序中导入本地包。

  1. type Post struct {
  2. URL string `json:"url,omitempty"`
  3. Caption string `json:"caption,omitempty"`
  4. Likes []User `json:"likes,omitempty"` // 无法从user包中导入User
  5. }
  1. type User struct {
  2. Name string `json:"name,omitempty"`
  3. Password string `json:"password,omitempty"`
  4. Followers []User `json:"followers,omitempty"`
  5. Followings []User `json:"followings,omitempty"`
  6. }
英文:

i have a problem. I cannot import local packages in my application.

  1. type Post struct {
  2. URL string `json:"url,omitempty"`
  3. Caption string `json:"caption,omitempty"`
  4. Likes []User `json:"likes,omitempty"` // Can not import User from package user
  5. }
  1. type User struct {
  2. Name string `json:"name,omitempty"`
  3. Password string `json:"password,omitempty"`
  4. Followers []User `json:"followers,omitempty"`
  5. Followings []User `json:"followings,omitempty"`
  6. }
  7. </details>
  8. # 答案1
  9. **得分**: 1
  10. 我为您的情景创建了一个示例结构,如下所示:
  11. 假设项目结构如下所示:
  12. project-villa/ //您的项目名称
  13. model/
  14. -user.go //此文件将包含您的用户结构
  15. repository/
  16. -post.go //此文件将保存您的帖子结构和其他代码片段
  17. handler/
  18. driver/
  19. main.go
  20. 步骤1:初始化模块
  21. go mod init project-villa
  22. 或者
  23. go mod init github.com/user-name/project-villa
  24. 模块将自行管理模块依赖关系。无论如何,如果它没有,您可以显式导入它。
  25. 它将如下所示:
  26. github.com/random/project-villa/models
  27. type Post struct {
  28. URL string `json:&quot;url,omitempty&quot;`
  29. Caption string `json:&quot;caption,omitempty&quot;`
  30. Likes []models.User `json:&quot;likes,omitempty&quot;` //您可以像这样使用它
  31. }
  32. 您可以参考官方Go开发文档中的[链接][1]。在这里,您将获得“从您的模块导入包”的信息。
  33. [1]: https://go.dev/doc/code
  34. <details>
  35. <summary>英文:</summary>
  36. I have created a sample structure for your scenario as follows:
  37. Assuming the Project Structure look like something this:
  38. project-villa/ //Name of your Project
  39. model/
  40. -user.go //this file will contain your User Structure
  41. repository/
  42. -post.go //this file will hold your Post structure and the rest piece of code
  43. handler/
  44. driver/
  45. main.go
  46. Step1:- initialize the module
  47. go mod init project-villa
  48. OR
  49. go mod init github.com/user-name/project-villa
  50. The mod will manage the module dependency itself. Anyhow if it doesn&#39;t you can import it explicitly.
  51. It will look like this:
  52. github.com/random/project-villa/models
  53. type Post struct {
  54. URL string `json:&quot;url,omitempty&quot;`
  55. Caption string `json:&quot;caption,omitempty&quot;`
  56. Likes []models.User `json:&quot;likes,omitempty&quot;` //you can use it like this
  57. }
  58. For the reference you can follow the
    [1] of official go dev. Here you will get the `Importing packages from your module`.
  59. [1]: https://go.dev/doc/code
  60. </details>

huangapple
  • 本文由 发表于 2021年12月2日 08:49:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/70192998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定