5

Go structure:

|--main.go
|
|--users
     |
     |---users.go

The two files are very simple: main.go:

package main

import "./users"

func main() {
    resp := users.GetUser("abcde")
    fmt.Println(resp)
}

users.go:

package users

import "fmt"

func GetUser(userTok string) string {
    fmt.Sprint("sf")
    return "abcde"
}

But it seems fmt is not accessible in main.go. When I try to run the program, it gives

undefined: fmt in fmt.Println

Anybody knows how to make fmt accessible in main.go?

Baiyu Liu
  • 61
  • 3
  • Go, is based on "has a" relationship, hence when using users in main, fmt package from users is not available to main and need to be imported explicitly in main. – Prashant Thakkar Dec 05 '15 at 04:51
  • 1
    You shouldn't use relative import paths if you want the standard build tools to work with your project. – JimB Dec 05 '15 at 14:07

1 Answers1

3

You need to import fmt in main as well.

Simply write "fmt" in import() in main.go and it should run.

import(    
    "fmt"  
    "./users"
)
Cactus
  • 27,075
  • 9
  • 69
  • 149
Mike
  • 3,830
  • 2
  • 16
  • 23