dongqian5639 2018-10-28 02:41
浏览 62

从本地子目录导入包

I'm new to go, so I hope this isn't perceived as dumb!

My current folder structure looks similar to this enter image description here

In the models folder, I have person.go.

package models

//Person struct
type Person struct {
    Name   string
    Age    int
    Gender string
}

In the main.go, I like to import models, so that I can use the person struct.

    package main

import "fmt"
import "models"

func main() {
    person = Person{Name: "Ali", Age: 34, Gender: "Male"}    
    fmt.Println("person is", person)
}

When I try execute go build, I get the following exception:

main.go:4:8: cannot find package "models" in any of: C:\Go\src\models (from $GOROOT) C:\Users\Ali\go\src\models (from $GOPATH)

I get that because my current working folder isn't my $GOROOT, nor is it the $GOPATH. In fact, I don't really want to add the models folder to either of those folders.

Is this even possible?

  • 写回答

1条回答 默认 最新

  • douzi2778 2018-10-28 06:25
    关注

    Use ./models instead of models.

    But the better solution is using go project directory structure.

    Read official docs

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭