专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Packages----Go组织项目结构-包

(英文阅读理解)

Every Go program is made up of packages.
Programs start running in package main.
This program is using the packages with import paths "fmt" and "math/rand".
By convention, the package name is the same as the last element of the import path. For instance, the "math/rand" package comprises files that begin with the statement package rand.
Note: The environment in which these programs are executed is deterministic, so each time you run the example program rand.Intn will return the same number.
(To see a different number, seed the number generator; see rand.Seed. Time is constant in the playground, so you will need to use something else as the seed.)

代码

package main
import (
    "fmt"
    "math/rand"
)
func main() {
    fmt.Println("My favorite number is", rand.Intn(10))
}

翻译

每个Go程序都是由package组成的.(这个地方的程序应该理解成最后打包成的一个可运行的东西,类似于jar,或者exe,sh文件等)
Go程序是以运行package main开始的,也就是说,Go开始运行的时候是寻找一个叫main的package,然后运行里面的main函数.
这个Go程序使用了fmt和math/rand包.
按照惯例,包名称和倒入路径的最后一个文件夹名字相同,例如:math/rand这个包名就叫rand

Note(注意):这个程序返回的Intn(10)是确定的一个数字,任何时候运行返回的数字都一样
(如果你想获取一个随机的数字,去看下数字种子(seed))

总结

这一个小节,其实就是扯了下,怎么导入pacakge,使用别人写好的东西,其实GoLand(一个开发Go的IDE工具)有提示,不用担心这个.

实验项目

我自己的小白鼠Go项目mouse项目,自己可以clone一份:github.com/liuxuzxx/mo…

文章永久链接:https://tech.souyunku.com/44593

未经允许不得转载:搜云库技术团队 » Packages----Go组织项目结构-包

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们