当前位置:网站首页>Introduction to go project directory structure
Introduction to go project directory structure
2022-06-30 07:03:00 【Duanxiaoman】
Preface
General programming languages are often used for engineering ( project ) There are no rules for the directory structure of , however Go Language has made relevant provisions in this regard , Let's talk about it Go Knowledge of language in engineering structure .
stay install Go Language development tools In this article we mentioned GOPATH This environment variable , It's a path , It is used to store the code package needed in development , The construction of the project is mainly realized by it , In other words , If you want to build a project , You need to add the directory of this project to the GOPATH in , Multiple projects can be used directly ; Separate , If you don't configure GOPATH, Even in the same directory , Codes cannot call each other through absolute paths .
Directory structure
It's going on Go When it comes to language development , Our code will always be stored in $GOPATH/src Under the table of contents , after go build、go install or go get After the order , The downloaded third-party package source code file will be placed in $GOPATH/src Under the table of contents , The generated binary executable is placed in $GOPATH/bin Under the table of contents , The generated intermediate cache file will be saved in the $GOPATH/pkg Under the table of contents .
One Go The directory of a language project generally contains the following three subdirectories :
- src Catalog : Source files for placing projects and Libraries ;
- pkg Catalog : Place the package generated after compilation / Archive of Library ;
- bin Catalog : Place the executable file generated after compilation ;
Of the three directories, we need to focus on src Catalog , Just know the other two directories , Let's introduce these three directories respectively .
src Catalog
Used to package (package) Organize and store Go Source file , The bag here is similar to src Each subdirectory under is one-to-one correspondence , for example , If a source file is declared to belong to log package , Then it should be kept in src/log Directory . Is not to say that src Cannot store in directory Go Source file , In general, when testing or demonstrating, you can also put Go The source file is placed directly in src Under the table of contents , But if you do, you can only declare that the source file belongs to main It's packed . In normal development, we still suggest that you put Go Put the source file in a specific directory .
Bag is Go The important mechanism of language management code , Its function is similar to Java Medium package and C/C++ The header file .Go The first valid code in the source file must be package < Package name > In the form of , Such as package hello.
Another thing to note ,Go Language will pass go get Download the library source file obtained by the command to src In the corresponding folder under the directory .
pkg Catalog
Used to store through go install Command the archive file after installing a package . Archive files are those whose names are “.a” Final document .
This catalog is related to GOROOT Catalog ( That is to say Go Language installation directory ) Under the pkg The directory function is similar to , The difference here is pkg Directory is an archive file specially used to store project code .
The process of compiling and installing project code is generally carried out in the unit of code package , such as log After the package is compiled and installed , Will generate a named log.a Archive file , And stored in the... Of the current project pkg Under the table of contents .
bin Catalog
And pkg Similar directory , Through go install After the command is installed , Save by Go Executable file generated by command source file . In the class Unix Under the operating system , The name of this executable file is the same as that of the command source file . And in the Windows Under the operating system , The name of the executable is the file name of the command source file plus .exe suffix .
Source file
Above, we mentioned the command source file and library source file , What exactly are they ?
- Command source file : If one Go The source file is declared to belong to main package , And the file contains main function , Then it is the command source file . The command source file belongs to the entry of the program , Can pass Go Linguistic go run Run or pass a command go build Command generates an executable file .
- Library source file : Library source files refer to ordinary source files that exist in a package , And the library source file does not contain main function .
Whether it's a command source file or a library source file , All source files in the same directory , The name of the package it belongs to must be consistent .
The current popular project structure
Go The language also organizes code files through packages , We can quote others' packages , You can also publish your own packages , But to prevent project name conflicts between different packages , We usually use Top-level domain name To prefix the package name , So I don't worry about the conflict of project names .
notes : Because not everyone has their own top-level domain name , So the popular way is Use personal github user name To distinguish between different packages .
The above is the current popular general project structure , What is the project structure when the company develops ?
So after understanding the structure of the project , You can be $GOPATH/src Under the directory, it is carried out according to the corresponding project structure Demo Developed .
边栏推荐
- ROS service communication programming
- 如果我在珠海,到哪里开户比较好?另外,手机开户安全么?
- Idea shortcut key
- [mask RCNN] target detection and recognition based on mask RCNN
- Four great happenings on earth
- RT thread Kernel Implementation (VI): time slice
- Porting RT thread to s5p4418 (V): thread communication
- [transfer] analysis of memory structure, cache and DMA architecture
- RT thread migration to s5p4418 (III): static memory pool management
- 第一行代码(第三版)学习笔记
猜你喜欢

Rising posture series: fancy debugging information

【Mask-RCNN】基于Mask-RCNN的目标检测和识别

Linux server installation redis

Out of class implementation of member function of class template
![[hot100] palindrome substring and longest palindrome substring](/img/a5/10dec640f02023c4d55cb42e6309fb.png)
[hot100] palindrome substring and longest palindrome substring

SOC_ AHB_ SD_ IF
![[datawhale team learning] task02: mathematical operation, string and text, list](/img/3e/c9c984d7576a2e410da7f81a8c0aa6.jpg)
[datawhale team learning] task02: mathematical operation, string and text, list

Installation du serveur linux redis
![[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network](/img/39/c9bf235828b6d50e7931876fa4a918.png)
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network

MySQL Optimization: from more than ten seconds to 300 milliseconds
随机推荐
【我的OpenGL学习进阶之旅】关于OpenGL的着色器的向量和矩阵分类的访问方式: xyzw/rgba/stpq以及数组下标
编写并运行第一个Go语言程序
踩坑记录:supervisor 日志返回信息:redis扩展未安装
2022年6月29日--使用C#迈出第一步--使用 C# 中的“if”、“else”和“else if”语句向代码添加决策逻辑
Graphic octet, really top
Why does ETL often become ELT or even let?
Rising posture series: fancy debugging information
leetcode:98. 验证二叉搜索树
【已解决】Failed! Error: Unknown error 1130
It turns out that you are such an array. You have finally learned
How does the CPU recognize the code?
What underlying technologies support the metauniverse?
B站首个UP主付费观看视频还是来了!价格“劝退”网友
【已实现】服务器jar包启动脚本、shell脚本
RT thread migration to s5p4418 (I): scheduler
Unity中实现溶解(Dissolve)特效及其原理解析
RT thread migration to s5p4418 (IV): thread synchronization
安装setup对应的组件
明天!“移动云杯”大赛空宣会开播!
Google Earth Engine(GEE)——墨累全球潮汐湿地变化 v1 (1999-2019) 数据集