Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang import package inside package

Tags:

package

go

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?

like image 822
Baiyu Liu Avatar asked Oct 18 '22 21:10

Baiyu Liu


1 Answers

You need to import fmt in main as well.

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

import(    
    "fmt"  
    "./users"
)
like image 62
Mike Avatar answered Oct 22 '22 02:10

Mike