3

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?

Ali Khakpouri
  • 801
  • 8
  • 24
  • 1
    Just stick to what How to Write Go Code tells you. There is no need to deviate from that. – Volker Oct 28 '18 at 07:34

1 Answers1

-1

Use ./models instead of models.

But the better solution is using go project directory structure.

Read official docs

Dariush Abbasi
  • 460
  • 1
  • 4
  • 15