Golang import package inside package

huangapple go评论92阅读模式
英文:

Golang import package inside package

问题

Go结构:

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

这两个文件非常简单:
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"
}

但是在main.go中似乎无法访问fmt。当我尝试运行程序时,会出现

undefined: fmt in fmt.Println

有人知道如何使main.go中的fmt可访问吗?

英文:

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?

答案1

得分: 3

你需要在main中也导入fmt

只需在main.goimport()中写入"fmt",然后它就可以运行了。

import(    
    "fmt"  
    "./users"
)
英文:

You need to import fmt in main as well.

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

import(    
    "fmt"  
    "./users"
)

huangapple
  • 本文由 发表于 2015年12月5日 12:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/34101360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定