-3

Here is my problem and my project structure

src
|-->config
       |--> config.go
|-->otherPackage
       |--> otherFile.go
|-->main.go

I have a type on config.go that I would like to use in otherFile.go

But when I tried to add it to the import here theses issues:

  1. imported and not used.
  2. undefined: Config

Although I use it in the function declaration

function(target float64, entries [2]float64, config Config)

What is the problem with this?

I tried to import it with

import (
    "fmt"
    "math"
    "../config"
)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
kevingiroux
  • 155
  • 1
  • 14

1 Answers1

1

You cannot "import from a package". All you can do is "import the whole package". That means if you import "full/import/path/of/foo" and that package declared itself to be called foo via package foo at the beginning, then everything in this package has to be qualified by foo:

foo.Config

If you package is called config than declaring a variable config will shaddow the whole package: so you have to:

  1. Rename the config variable to e.g. cfg
  2. Reference Config from package config with its qualified name config.Config
Volker
  • 40,468
  • 7
  • 81
  • 87
  • Sorry, i didn't understand well. I just have to rename the variable on my otherFile or on the config.go file ? – kevingiroux Mar 23 '18 at 07:02
  • @kevingiroux you can do this `import cfg "path/to/config"`. You can then refer to that package as `cfg` i.e. `cfg.Config`, `cfg.Var1`, `cfg.Var2`, etc. – Pandemonium Mar 23 '18 at 07:28
  • The best advice is: Work through the Tour of Go once more. Then read some other packages and understand what's going on. You'll have to build understanding, not apply mechanical fixes. – Volker Mar 23 '18 at 07:29
  • It seems to be another problem, my GoPath is not well configured. When i try echo $GOPATH i have $Home/Document/workspace/go But i search on $Home/go ... – kevingiroux Mar 23 '18 at 07:36
  • 1
    Then read How to Write Go Code and _stick_ to it _word_ _for_ _word_. – Volker Mar 23 '18 at 08:28