Go language from 0-19|Go theme month

Engineering Management

Continue to learn about engineering management in Go language. I've been working overtime lately!

engineering organization

The project organization management of golang completely uses the directory structure and package name to deduce the project structure and build order.

GOPATH

The GOPATH environment variable is something that must be mentioned before discussing engineering organization. Most of the functions of Gotool are no longer for the current directory, but for the package name, so how to locate the corresponding source code falls on GOPATH. The literal understanding is to find the package name path through GOPATH, so as to locate the source code that needs to be depended on.

Suppose you have two Go project codes. For example, my project is generally read and placed in the work directory under the local Documents. /Users/leixiaohong/Documents/work/go-juejin1Another Go project is in /Users/leixiaohong/Documents/gowork/go-juejin2the GOPATH can be set to:

export GOPATH=/Users/leixiaohong/Documents/work/go-juejin1:/Users/leixiaohong/Documents/gowork/go-juejin2
复制代码

After this setting, you can build the above two projects at any location. In fact, I think it is a bit like configuring environment variables.

Directory Structure

A standard Go language project contains the following directories: src, pkg and bin. The directory src is used to contain all the source code, which is a mandatory rule of Gotool, while pkg and bin do not need to be created manually. If necessary, Gotool will automatically create these directories during the build process.

<calcproj> 
    ├─README 
    ├─AUTHORS 
    ├─<bin> 
        ├─calc 
    ├─<pkg> 
        └─<linux_amd64> 
            └─simplemath.a 
    ├─<src> 
        ├─<calc> 
            └─calc.go 
        ├─<simplemath> 
            ├─add.go 
            ├─add_test.go 
            ├─sqrt.go 
            ├─sqrt_test.go
复制代码
  • README: All open source projects have README, which is mainly a brief introduction to the project. Usually the first thing to look at the project is to read this document.
  • LICENSE: The distribution protocol adopted by this project. All open source projects usually have this file.
  • src: All the source code, for example, means that the original code is divided into calctwo packages and simplemaththe xxx.go source code stored in it respectively.
  • bin&pkg: Generally, bin and pkg directories can be created without creating them, and the go command will create them automatically.

document management

For programmers, what we call documentation refers to comments, functions, and interface input, output, function, and parameter descriptions in the code, which play a crucial role in subsequent maintenance and reuse. Write the comment and execute the godoc -http=:90 -path="."command directly, and then access the link in the browser http://localhost:90/to see the commented page document. To extract comments as documents, follow the basic rules below.

  • 注释需要紧贴在对应的包声明和函数之前,不能有空行
  • 注释如果要新起一个段落,应该用一个空白注释行隔开,因为直接换行书写会被认为是正常的段内折行。
  • 开发者可以直接在代码内用// BUG(author): 的方式记录该代码片段中的遗留问题,这些遗留问题也会被抽取到文档中。

其实我觉得对于程序员来说最好的注释文档就是看代码,代码简洁到能看懂就ok。Go语言在这方面做的足够好。

工程构建

在有GOPATH配置的前提下,直接用go build命令来执行构建,但需要注意的是,在你构建可执行程序工程时,会在你所在的目录中生成可执行程序。如果你不希望calc到处都是,就选择一个你期望的目录, 比如go-juejin1目录下的bin目录。

$ go build calc
复制代码

下一步是将构建成功的包安装到恰当的位置,具体指令如下:

$ go install calc
复制代码

如果之前没有执行过go build命令,则go install会先执行构建,之后将构建出来的calc可执行文件放到bin目录下。如果目标工程是一个包,则会放置到pkg目录中对应的位置 pkg/linux_amd64/simplemath.a:

$ go install simplemath
复制代码

后话

先写到这里,其实这个活动快到尾声了,说点心里话吧其实参加这个活动开始是看上奖品,其实有想过去刷题,但是我对Go语言语法不懂,对我自身没有任何好处,所以我选择看书,然后再加上自己的小小理解,写文章,写的很多不对的地方见谅,但是对我来说我多少学到了很多Go语言的知识,Go的函数多返回值,和对Error的处理,对我印象很深刻,包括Go的多线程编程,线程之间的通信等都让我眼前一亮,我发现自己很多知识知道但是不知道怎么表达出来,我准备在掘金把我表达不出来的java知识写出来这是后话,看书写文章,然后通过写文章把知识讲出来,来达到理解的程度。也是监督自己读书的一个习惯吧。自己看一遍,再写一遍,然后发布的时候我为了检查错别字还需要看一遍文章,一个知识点读三遍,比看一遍有作用多了吧!好了废话凑的都快400字了,闪了。

备注

本文正在参与「掘金Golang主题学习月」, 点击查看活动详情

Guess you like

Origin juejin.im/post/6954576579808722952