go语言详解-----包(package)引入

一、go语言基础

1、每一个可运行的go程序必须要有一个main 包(package),即package main;并且该main包中必须要有一个main函数,这是一个入口函数。

2、一个包中的描述符要能够被其他的包引用,那么该包中的相关描述符的首字母一定要大写

二、 GOPATH 模式 (GO111MODULE=off)

1、每一个源码文件必须属于一个包

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)
  

2、dot(.) 别名引入

import (
    "fmt"   
   .  "./yyzc"
)

这种方式引入,可以不使用别名.函数名方式引用函数,可以直接引用函数名,但是要避免函数名重复。

3、一个文件夹下的*(不包括子文件夹)所有源文件必须属于同一个包,否则会报如下错

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
}

4、同一个目录下(不含子目录)的源码,即同一个包(package)中的源码中全局变量要唯一

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
}


5、同一个go项目中,如果出现了相同的包(package)名,需要使用别名来区分,类似如下:

import (
    "fmt" 
    "../test"
  greet   "../test/yyzc"
)

6、go语言在包引入的时候,引入的是一个路径,即目录

相对路径引入

import ( 
  "../test"
greet   "../test/yyzc"
)

此种引入方式,会到对应的相对路径下检测,首先检测该路径(不包含子路径)下的所有源文件是否属于同一个包(package),如果不是,则会报错,如果是则会标记该目录为packagename名,也可以使用别名引用 ,别名引用:

import ( 
greet   "../test/yyzc"
)

标准引入

import (
    "fmt" 
  
)

标准引入方式,会去到 $GOPATH\src$GOROOT\src 路径下检查此目录,如果没有此目录则会报错,如果有次目录则会遍历该目录(不含子目录)中所有的文件,提取包名,并引用此包名。

7、一个目录所形成的包与其子目录所形成的包(package)是不一样的,即包是以目录为单位的,不包含其子目录 ,如果目录与其子目录包(package)名一样,则会报错,可以用上述方法避免错误。或者直接使用不一样的包名

三、module-aware模式(GO111MODULE=on)

这种模式下,相对路径引入会报如下错误

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>

那么如果非 $GOPATH\src 路径下的包该如何引用呢?

1、main包所在的目录下需要有一个go.mod文件,可以使用go mod init modname 初始化一个mod文件,否则会报错

2、被引入的本地库也要有一个go.mod文件

3、引用其他包的描述符(例如函数),要使用packagename.函数名

猜你喜欢

转载自blog.csdn.net/qq_41768644/article/details/132766453