初始化
This commit is contained in:
3
01_basics/06_packages/go.mod
Normal file
3
01_basics/06_packages/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module packages
|
||||
|
||||
go 1.21
|
13
01_basics/06_packages/main.go
Normal file
13
01_basics/06_packages/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"packages/myutils" // 导入自定义包
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 调用外部包中的函数
|
||||
myutils.Greet("Gopher")
|
||||
|
||||
fmt.Println("包导入和使用示例")
|
||||
}
|
13
01_basics/06_packages/myutils/utils.go
Normal file
13
01_basics/06_packages/myutils/utils.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package myutils
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 首字母大写的函数才能被外部包访问
|
||||
func Greet(name string) {
|
||||
fmt.Printf("Hello, %s!\n", name)
|
||||
}
|
||||
|
||||
// 小写函数只能在包内使用
|
||||
func internalFunc() {
|
||||
fmt.Println("This is internal")
|
||||
}
|
Reference in New Issue
Block a user