首页 > 其他分享 >【Go】001. 工程管理

【Go】001. 工程管理

时间:2024-04-12 11:45:39浏览次数:17  
标签:GOPATH 工程 get module go 001 install Go

一般在介绍语言的书中不会出现工程管理的内容,但只要讲到Go语言,我们就不应该把语法和工程管理区分开。因为Go语言在设计之初就考虑了在语言层面如何更好地解决当前工程管理中的一些常见问题,而自带的Go工具则更是从工程管理的方方面面来考虑,并提供了完善的功能。让学习者在学习语言阶段自然而然地养成了工程的习惯,避免出现学院派和工程派两种明显不同的侧重点。
归根到底,Go语言是一门工程语言。

1. Go 命令行工具

可以完成以下几类工作:

  • 代码格式化
  • 代码质量分析和修复
  • 单元测试与性能测试
  • 代码文档的提取和展示
  • 依赖包管理
  • 执行其它的包含指令,比如6g蹬

示例:

C:\Users\小白一生>go help
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.

go get

go get 命令用于从远程代码仓库(如 GitHub 或 GitLab)下载 Go 语言的包及其依赖,并将其安装到本地的 Go 开发环境中。从 2019 年开始,随着 Go Modules 功能的普及和默认启用,go get 的行为有所变化:

  1. 对于使用 Go Modules 的项目(Go 1.11 版本以后),当你执行 go get 命令时,它会根据 go.mod 文件管理依赖,并将依赖包下载至 $GOPATH/pkg/mod 目录下(如果设置了 GOPATH)。同时,go get 也会在全局模块缓存中存储依赖,通常位于 ~/.cache/go-build(对于 Unix 系统)或 %USERPROFILE%\.cache\go-build(对于 Windows 系统)。

  2. 如果你是在一个非模块化的项目(或者禁用了模块功能)并且设置了 GOPATH,那么下载的源码将会存储在 GOPATH/src 目录下相应的位置,例如:

    GOPATH=/home/user/go
    

    执行 go get github.com/user/repo 后,源码会被放在:

    /home/user/go/src/github.com/user/repo
    
  3. 对于可执行程序(比如 main 包),如果使用 Go Modules,它们会被安装到一个与项目相关的临时目录(由 Go Modules 管理),而不再是传统的 GOPATH/bin。若想将可执行文件安装到某个特定目录,可以使用 go install 命令配合 -i 标志(现在 -i 不再需要,因为 go install 默认包含了构建和安装的动作)和 -o 标志指定输出路径。

  4. 如果需要查看已安装到可执行路径下的二进制文件,可以通过 go list -f '{{.Target}}' <package> 查看对应包编译后的二进制文件路径。

  5. 在 Go 1.13 版本后,为了更细粒度地控制依赖,推荐使用 go install 命令替代 go get 来安装独立的命令行工具或库。

总之,go get 下载的包位置与项目的 Go Modules 设置和全局 GOPATH 设置紧密相关。随着 Go 语言的发展,最佳实践也在不断演进,因此建议始终查阅最新的官方文档了解最新行为。

2. 代码风格

“代码必须是本着写给人阅读的原则来编写,只不过顺便给机器执行而已。”
Go语言代码风格强制统一。

命名

命名规则涉及变量、常量、全局函数、结构、接口、方法等的命名。Go语言语法层面限定如下:

  • 任何需要对外暴露的名字必须以大写字母开头,不需要对外暴露的则应该以小写字母开头。
  • 拥护骆驼命名法而排斥下划线法

排列

go fmt代码格式化工具,不加参数调整当前目录所有go文件,可跟指定包名

  • 调整语句位置
  • 重新拜访花括号位置
  • 以制表符缩进代码
  • 添加空格

导包

package main

import (
    "fmt"
	"github.com/mytem/exp/crc32"
)

导入远程包执行go buildgo install之前需要通过go get拉取包

标签:GOPATH,工程,get,module,go,001,install,Go
From: https://www.cnblogs.com/liudianer/p/18130838

相关文章

  • 【Go】go get & go mod
    gogetgoget命令用于从远程代码仓库(如GitHub或GitLab)下载Go语言的包及其依赖,并将其安装到本地的Go开发环境中。从2019年开始,随着GoModules功能的普及和默认启用,goget的行为有所变化:对于使用GoModules的项目(Go1.11版本以后),当你执行goget命令时,它会根据......
  • 脑洞golang embed 的使用场景
    golang的embed的功能真是一个很神奇的功能,它能把静态资源,直接在编译的时候,打包到最终的二进制程序中。为什么会设计这么一个功能呢?我想和golang的崇尚简单的原则有关系吧。它希望的是一个二进制文件能走天下,那么如果你作为一个web服务器,还需要依赖一大堆的静态文件,终究不......
  • Godot Label样式 Textrue纹理,实现样式修改,背景填充
    目录前言运行环境新建项目Style样式讲解StyleBoxEmpty:普通样式StyleBoxTexture:字体样式StyleBoxFlat:填充样式StyleBoxLine:行样式总结前言在Godot中,直接的BackGroud背景颜色这个属性。Godot中使用的是Textrue纹理这个属性来表示文本的信息运行环境Godot4.2.1Windows10......
  • golang JSON序列化和反序列化
    目录JSON序列化(Marshaling)JSON反序列化(Unmarshaling)错误处理和注意事项在Go语言(通常被称为Golang)中,JSON(JavaScriptObjectNotation)是一种常用的数据交换格式。Go标准库提供了encoding/json包,使得JSON的序列化(将Go数据结构转换为JSON格式的字符串)和反序列化(将JSON格式的字符串......
  • 52 Things: Number 23: Write a C program to implement Montgomery arithmetic.
    52Things:Number23:WriteaCprogramtoimplementMontgomeryarithmetic.52件事:第23件:写一个C程序来实现Montgomery算术。 Thisisthelatestinaseriesofblogpoststoaddressthelistof这是一系列博客文章中最新的一篇'52ThingsEveryPhDStudentShould......
  • 52 Things: Number 22: How do you represent a number and multiply numbers in Mont
    52Things:Number22:HowdoyourepresentanumberandmultiplynumbersinMontgomeryarithmetic?52件事:数字22:在蒙哥马利算术中,你如何表示一个数字并将其相乘? Thisisthelatestinaseriesofblogpoststoaddressthelistof '52ThingsEveryPhDStudentSh......
  • 52 Things: Number 24: Describe the binary, m-ary and sliding window exponentiati
    52Things:Number24:Describethebinary,m-aryandslidingwindowexponentiationalgorithms.52件事:第24件:描述二进制、m进制和滑动窗口求幂算法。 Thisisthelatestinaseriesofblogpoststoaddressthelistof '52ThingsEveryPhDStudentShouldKnow......
  • 52 Things: Number 26: Describe the NAF scalar multiplication algorithm.
    52Things:Number26:DescribetheNAFscalarmultiplicationalgorithm.52件事:第26件:描述NAF标量乘法算法。 Thisisthelatestinaseriesofblogpoststoaddressthelistof '52ThingsEveryPhDStudentShouldKnow' todoCryptography:asetofquestion......
  • 52 Things: Number 15: Key generation, encryption and decryption algorithms for R
    52Things:Number15:Keygeneration,encryptionanddecryptionalgorithmsforRSA-OAEPandECIES.52件事:第15件:RSA-OAEP和ECIES的密钥生成、加密和解密算法。 Thisisthelatestinaseriesofblogpoststoaddressthelistof  '52ThingsEveryPhDStuden......
  • 52 Things: Number 16: Describe the key generation, signature and verification al
    52Things:Number16:Describethekeygeneration,signatureandverificationalgorithmsforDSA,SchnorrandRSA-FDH.52件事:第16件:描述DSA、Schnorr和RSA-FDH的密钥生成、签名和验证算法。 Thisisthelatestinaseriesofblogpoststoaddressthelistof'......