Go教程

golang package和module使用

本文主要是介绍golang package和module使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!


Go has no sub-packages

When you want to split your packages to organize your BIG package, you may make some of the internals of your package visible to the outside world, and so, anyone can import them. You may not want this.

Internal package convention prevents your packages to be imported from unwanted parties. I explain internal packages here in this post.

 

Put tests into the same directory

Do not put your testing code into separate test directories.

Keep them together like: miner.go and miner_test.go into the same directory, they live happily there together.

Put the data needed for testing into testdata directories as a sub-directory. Go tools will respect that convention.

miner/
    miner.go
    miner_test.go
    testdata/
        hashes.data
        .


Use common abbreviations

Only if they’re familiar to the programmers (or to the programmers in the domain of the application you’re building). For example: Instead of formattedIO, stdlib uses fmt.

Avoid generic package names

Not like this: api, models, common, utils, helper etc.

For example: Instead of a model package, define a package that does user things named as user. Or, define another package for a client order as order.

Do not put everything inside a models folder, split them into more smaller packages.

However, this goes to some extent. If your separate packages need some relationship between them, you may cluster them together depending on your goal.

Do not use underscores or camel-casing

Just use lowercase letters.





参考:https://blog.learngoprogramming.com/code-organization-tips-with-packages-d30de0d11f46



Nested Packages in Go 

go中嵌套包

Example

Create a directory named learn. Let’s create a module with import path as “sample.com/learn” in the learn directory.

go mod init sample.com/learn

It will create a go.mod file

go.mod

module sameple.com/learn

go 1.14

Let’s create below files and directories

  • learn/main.go
  • learn/math/math.go
  • learn/math/advanced/advanced.go

You can see that advanced is a nested package inside the math package.

learn/math/math.go

package math
func Add(a, b int) int {
    return a + b
}
func Subtract(a, b int) int {
    return a - b
}

learn/math/advanced/advanced.go

package advanced
func Square(a int) int {
    return a * a
}

learn/main.go

package main
import (
    "fmt"
    "sample.com/learn/math"
    "sample.com/learn/math/advanced"
)
func main() {
    fmt.Println(math.Add(2, 1))
    fmt.Println(math.Subtract(2, 1))
    fmt.Println(advanced.Square(2))
}

Let’s run this program

learn $ go install
learn $ learn
3
1
4

Points to note about above program

  • We imported the advanced package in main.go with full qualified path i.e,  import “sample.com/learn/math/advanced”
  • Square function is referred to using advanced package i.e, advanced.Square(2)

注意点:导入子包必须是子包的完全路径。 另外使用子包也是子包的名字

 

https://golangbyexample.com/nested-packages-golang/

https://www.cnblogs.com/dcz2015/p/10763254.html

https://www.callicoder.com/golang-packages/





这篇关于golang package和module使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!