package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
import (
"fmt"
. "./yyzc"
)
这种方式引入,可以不使用别名.函数名方式引用函数,可以直接引用函数名,但是要避免函数名重复。
PS C:\Users\love1\Documents\technology\go\project1\hello> go run hello.go
hello.go:6:5: found packages greeting (greet1.go) and greetings (greetings.go) in C:\Users\love1\Documents\technology\go\project1\test
greetings.go
package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message
}
greet1.go
package greeting
import "fmt"
// Hello returns a greeting for the named person.
func HelloWolrd(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Hello World!", name)
return message
}
PS C:\Users\love1\Documents\technology\go\project1\hello> go run hello.go# /C/Users/love1/Documents/technology/go/project1/test
…\test\greetings.go:6:6: Hello redeclared in this block
…\test\greet1.go:6:6: other declaration of Hello
PS C:\Users\love1\Documents\technology\go\project1\hello>
greetings.go
package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message
}
greet1.go
package greetings
import "fmt"
// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Hello World!", name)
return message
}
import (
"fmt"
"../test"
greet "../test/yyzc"
)
相对路径引入
import (
"../test"
greet "../test/yyzc"
)
此种引入方式,会到对应的相对路径下检测,首先检测该路径(不包含子路径)下的所有源文件是否属于同一个包(package),如果不是,则会报错,如果是则会标记该目录为packagename名,也可以使用别名引用 ,别名引用:
import (
greet "../test/yyzc"
)
标准引入
import (
"fmt"
)
标准引入方式,会去到 $GOPATH\src 和 $GOROOT\src 路径下检查此目录,如果没有此目录则会报错,如果有次目录则会遍历该目录(不含子目录)中所有的文件,提取包名,并引用此包名。
这种模式下,相对路径引入会报如下错误
package main
import (
"fmt"
"../test"
greet "../test/yyzc"
)
func main() {
// Get a greeting message and print it.
message := greetings.Hello("Gladys")
fmt.Println(message)
message1 := greet.Hello("Gladys")
fmt.Println(message1)
}
S C:\Users\love1\Documents\technology\go\project1\hello> go run hello.go
hello.go:6:5: “…/test” is relative, but relative import paths are not supported in module mode
hello.go:7:3: “…/test/yyzc” is relative, but relative import paths are not supported in module mode
PS C:\Users\love1\Documents\technology\go\project1\hello>
将上述文件夹test放在 $GOROOT\src 路径下,再去引用,就不会报错
package main
import (
"fmt"
"test"
greet "test/yyzc"
)
func main() {
// Get a greeting message and print it.
message := greetings.Hello("Gladys")
fmt.Println(message)
message1 := greet.Hello("Gladys")
fmt.Println(message1)
}
但是如果将文件夹test放在 $GOPATH\src 路径下,再去引用,会报如下错误
PS C:\Users\love1\Documents\technology\go\project1\hello> go run hello.go
hello.go:6:5: package test is not in GOROOT (C:\Program Files\Go\src\test)
hello.go:7:3: package test/yyzc is not in GOROOT (C:\Program Files\Go\src\test\yyzc)
PS C:\Users\love1\Documents\technology\go\project1\hello>