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?

2
  • 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. Commented Dec 5, 2015 at 4:51
  • 1
    You shouldn't use relative import paths if you want the standard build tools to work with your project. Commented Dec 5, 2015 at 14:07

1 Answer 1

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"
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.