《Go语言程序设计》 0 包和go工具

感觉这个应在放在最前面,所以把序号改成0了

引言

  • 每个包定义了一个不同的命名空间作为它标识符
  • 包通过控制名字是否导出使其对包外可见来提供封装的能力
  • 修改一个文件时,必须重新编译文件所在的包和所有潜在依赖它的包
  • Go语言编译快的原因
    • 导入在源文件的开头显式的列出
    • 包的依赖形成有向无环图,各包可以独立甚至并行编译
    • 每个包编译输出的目标文件不仅记录自己的导出信息,还记录了其依赖包的导出信息

导入路径

包的导入路径指的是一个唯一标识包的字符串,它表示了包在文件系统中的位置和包的名称,用于import声明中

import "github.com/user/repo/pkg"
import "path/to/pkg"
  • 对于准备共享或公开的包,导入路径需要全局唯一。为了避免冲突,除了标准库中的包,其他包的导入路径应该以互联网域名座位路径的开始
  • 如果包是在本地文件系统中,导入路径可以是绝对或相对路径

包的声明

在每一个Go源文件的开头都需要进行包声明,表明该文件属于的包。通常,包名是导入路径的最后一段

导入声明

使用import进行导入

  • 导入的包可以通过空行来分组
import (
	"fmt"
	"os"

	"golang.org/x/net/html"
)
  • 名字一样的包,导入时其中一个要重命名
import (
	"crypto/rand"
	mrand "math/rand
)

空导入

如果导入的包没有在文件中引用,就会产生一个编译错误
但有时我们导入包是为了其副作用:对包级别变量执行初始化表达式求值,执行其init()函数
此时使用_进行一个重命名导入

import _ "image/png"

go工具

Go is a tool for managing Go source code.

Usage:

        go <command> [arguments]

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         add dependencies to current module and install them
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        work        workspace maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

        buildconstraint build constraints
        buildmode       build modes
        c               calling between Go and C
        cache           build and test caching
        environment     environment variables
        filetype        file types
        go.mod          the go.mod file
        gopath          GOPATH environment variable
        gopath-get      legacy GOPATH go get
        goproxy         module proxy protocol
        importpath      import path syntax
        modules         modules, module versions, and more
        module-get      module-aware go get
        module-auth     module authentication using go.sum
        packages        package lists and patterns
        private         configuration for downloading non-public code
        testflag        testing flags
        testfunc        testing functions
        vcs             controlling version control with GOVCS

Use "go help <topic>" for more information about that topic.

工作环境组织

找到一个文件夹,将其路径设置为GOPATH环境变量。该文件夹下应包含三个子目录

  • $GOPATH/src用于包含源文件,每一个包放在其下面的一个子目录中,且该子目录相对于$GOPATH/src的路径就是包的导入路径
  • $GOPATH/pkg子目录是构建工具存储编译后包的位置
  • $GOPATH/bin子目录防止可执行程序

包的下载

go get 导入路径,比如

go get github.com/golang/lint/golint

包的构建

没看懂这破书写了一通什么东西,查看这个网站的教程:

go build命令(go语言编译命令)完全攻略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值