GO语言多级目录包


有个学生管理系统

假设GPATH目录是

D:\Project\go\

有两个应用包 D:\Project\go\src\student\profile.go 学生属性

package student
func Say() string{
    return "I am student profile"
}

D:\Project\go\src\teacher\profile.go 教师属性

package teacher
func Say() string {
    return "I am teacher profile"
}

学生管理系统应用程序入口包是
D:\Project\go\src\manage\main.go

package main
import (
    "fmt"
    "lib/student"
    "lib/teacher"
)
func main(){
    fmt.Println("Hello student manage system")
    fmt.Println(student.Say())
    fmt.Println(teacher.Say())
}

Say 方法必须是开头字母大写,应为go规定开头字母大写才是public属性 可以外部调用

D:\Project\go\src\manage\ 目录下执行 go run ./main.go
    D:\Project\go\src\manage> go run .\main.go
    Hello student manage system
    I am student profile
    I am teacher profile
    PS D:\Project\go\src\manage>

GO语言多级目录包介绍到这里,更多Go学习请参考编程字典Go教程 和问答部分,谢谢大家对编程字典的支持。


原文链接:https://blog.csdn.net/aibiancheng_/article/details/100053711?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163504077416780264068914%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=163504077416780264068914&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_v2~rank_v29-1-100053711.pc_v2_rank_blog_default&utm_term=go&spm=1018.2226.3001.4450