资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

go语言godef go语言和python哪个好

GO语言(二十五):管理依赖项(上)-

当您的代码使用外部包时,这些包(作为模块分发)成为依赖项。随着时间的推移,您可能需要升级或更换它们。Go 提供了依赖管理工具,可帮助您在合并外部依赖项时确保 Go 应用程序的安全。

我们提供的服务有:成都网站制作、网站设计、微信公众号开发、网站优化、网站认证、青岛ssl等。为上千多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的青岛网站制作公司

本主题介绍如何执行任务以管理您在代码中承担的依赖项。您可以使用 Go 工具执行其中的大部分操作。本主题还介绍了如何执行其他一些您可能会觉得有用的依赖相关任务。

您可以通过 Go 工具获取和使用有用的包。在 pkg.go.dev 上,您可以搜索您可能觉得有用的包,然后使用go命令将这些包导入您自己的代码中以调用它们的功能。

下面列出了最常见的依赖项管理步骤。

在 Go 中,您将依赖项作为包含您导入的档核包的模块来管理。此过程由以备蠢顷下机构支持:

您可以搜索pkg.go.dev以查找具有您可能觉得有用的功能的软件包。

找到要在代码中使用的包后,在页面顶部找到包路径,然后单击复制路径按钮将路径复制到剪贴板。在您自己的代码中,将路径粘贴到导入语句中,如下例所示:

在您的代码导入包后,启用依赖项跟踪并获取包的代码进行编译。

要跟踪和管理您添加的依赖项,您首先要将代码放入其自己的模块中。这会在源代码树的根目录创建一个 go.mod 文件。您添加的依赖项将列在该文件中。

要将您的代码添加到它自己的模块中,请使用 go mod init命令。例如,从命令行切换到代码的根目录,然后按照以下示例运行命令:

该go mod init命令的参数是您的模块的模块路径。如果可能,模块路径应该是源代码的存储库位置。

如果一开始您不知道模块的最终存储库位置,请使用安全的替代品。这可能是您拥有的域的名称或您控制的另一个名称(例如您的公司名称),以及来自模块名称或源目录的路径。

当您使用 Go 工具管理依赖项时,这些工具会更新 go.mod 文件,以便它维护您的依赖项的当前列表。

添加依赖项时,Go 工具还会创建一个 go.sum 文件,其中包含您所依赖的模块的校验和。Go 使用它来验证下载的模块文件的完整性,特别是对于在您的项目上工作的其他开发人员。

在代码中包含存储库中的 go.mod 和 go.sum 文件。

当您运行go mod init创建用于跟踪依赖项的模块时,您指定一个模块路径作为模块的名称。模块路径成为模块中包的导入路径前缀。一定要指定一个不会与其他模块的模块路径冲突的模块路径。

至少,一个模块路径只需要表明它的来源,例如公司或仿陆作者或所有者名称。但是路径也可能更能描述模块是什么或做什么。

模块路径通常采用以下形式:

1、Go 工具可以在其中找到模块源代码的存储库的位置。

例如,它可能是github点抗 / /.

如果您认为您可能会发布模块供其他人使用,请使用此最佳实践。

2、一个你控制的名字。

如果您不使用存储库名称,请务必选择一个您确信不会被其他人使用的前缀。一个不错的选择是您公司的名称。避免使用常用术语,例如widgets、utilities或 app。

Go 保证以下字符串不会在包名称中使用。

1、test– 您可以将test用作模块路径前缀以便代码用于在另一个模块中本地测试功能进行测试。

使用test作为模块路径前缀是测试的一部分。例如,您的测试本身可能会运行go mod init test,然后以某种特定方式设置该模块,以便使用 Go 源代码分析工具进行测试。

2、example– 在某些 Go 文档中用作模块路径前缀,例如在创建模块以跟踪依赖关系的教程中。

请注意,Go 文档还用于example点抗 说明示例何时可能是已发布的模块。

go语言是什么

Go语言是一种开源的编程语言,被广泛应用于网络编程、云计算、分布式系统等领域。

go语言的三位作者

Go语言的设计目标是成为一种语法简洁、执行效率高、并发性能强大的编程语言。它由Google公司研发,于2009年首次发布,并于2012年成为了开源项目。Go语言具有C语言的表达能力和Python的开发效率,同时还拥有自己独特的语法和特性,如协程、垃圾回收机制等。因此,它被广泛应用于网络编程、云计算、分布式系统等领域,并且越来越受到开发者的青睐。

Go语言的出现,填补了许多编程语言在并发编程方面的空缺。它提供了一种轻量级线程模型,通过协程碧虚亩(goroutine)的方式,实现了高效的并发编程。誉颤同时,Go语言还支持内置的网络编程和字节序列编解码库,使得网络编程变得更加容易和高效。在云计算、分布式悔森系统等领域,Go语言也得到了广泛的应用。例如,Docker和Kubernetes等开源项目就是用Go语言开发的。此外,Go语言还具有代码可读性高、编译速度快、编译后的可执行文件体积小等优点,使得它成为了开发高性能、高并发应用的理想语言之一。

如何配置go语言集成开发环境 vim

1、编译vimgdb

下载vimgdb73和vim73

mkdir -p ./tmp

cd tmp

tar zxvf ../vim-7.3.tar.gz

unzip ../vimgdb-for-vim7.3-master.zip

mv vimgdb-for-vim7.3-master vimgdb-for-vim7.3

patch -p0 vimgdb-for-vim7.3/vim73.patch

cd vim73

安装依赖

sudo apt-get install build-essential

sudo apt-get build-dep vim-gtk

sudo apt-get install libncurses5-dev

安装

// 这里直接执行make的操作

make

sudo make install

安装vimgdb runtime

cd ../vimgdb-for-vim7.3

cp vimgdb_runtime ~/.vim/bundle

打开vim

:helptags ~/.vim/bundle/vimgdb_runtime/doc " 生成doc文件

添加配置.vimrc

" vimgdb插件派孝

run macros/gdb_mappings.vim

在vim中执行gdb时,报 “Unable to read from GDB pseudo tty” 的错误,因为没有安装 gdb ,所以安装gdb

sudo apt-get install gdb

2、安装vundle

set up vundle

$ git clone ~/.vim/bundle/vundle

Configure Plugins

在.vimrc文件的开头添加下面的内容,有些不是必须的亏裂,可以注掉

set nocompatible " be iMproved, required

filetype off " required

" set the runtime path to include Vundle and initialize

set rtp+=~/.vim/bundle/vundle/

call vundle#rc()

" alternatively, pass a path where Vundle should install plugins

"let path = '~/some/path/here'

"call vundle#rc(path)

" let Vundle manage Vundle, required

Plugin 'gmarik/vundle'

" The following are examples of different formats supported.

" Keep Plugin commands between here and filetype plugin indent on.

" scripts on GitHub repos

Plugin 'tpope/vim-fugitive'

Plugin 'Lokaltog/vim-easymotion'销羡闭

Plugin 'tpope/vim-rails.git'

" The sparkup vim script is in a subdirectory of this repo called vim.

" Pass the path to set the runtimepath properly.

Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}

" scripts from

Plugin 'L9'

Plugin 'FuzzyFinder'

" scripts not on GitHub

Plugin 'git://git.wincent点抗 /command-t.git'

" git repos on your local machine (i.e. when working on your own plugin)

Plugin ''

" ...

filetype plugin indent on " required

" To ignore plugin indent changes, instead use:

"filetype plugin on

"

" Brief help

" : PluginList - list configured plugins

" : PluginInstall(!) - install (update) plugins

" : PluginSearch(!) foo - search (or refresh cache first) for foo

" : PluginClean(!) - confirm (or auto-approve) removal of unused plugins

"

" see :h vundle for more details or wiki for FAQ

" NOTE: comments after Plugin commands are not allowed.

" Put your stuff after this line

Install Plugins

Launch vim and run

: PluginInstall

vim +PluginInstall +qall

3、官方vim-lang插件

Config vim file .vimrc,Add content bellow in bottom of the file

" 官方的插件

" Some Linux distributions set filetype in /etc/vimrc.

" Clear filetype flags before changing runtimepath to force Vim to

" reload them.

filetype off

filetype plugin indent off

set runtimepath+=$GOROOT/misc/vim

filetype plugin indent on

syntax on

autocmd FileType go autocmd BufWritePre Fmt

4、代码补全的插件gocode

配置go的环境变量,比如我的配置,GOPATH变量是必须要配置的,PATH中必须把GOPATH的bin也添加进去,否则没有自动提示,会提示找不到模式

export GOROOT=/usr/local/go

export GOPATH=/data/app/gopath

export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Set up gocode

Then you need to get the appropriate version of the gocode, for 6g/8g/5g compiler you can do this:

go get -u github点抗 /nsf/gocode (-u flag for "update")

Configure vim in .vimrc file

Plugin 'nsf/gocode', {'rtp': 'vim/'}

Install Plugins

Launch vim and run

: PluginInstall

vim +PluginInstall +qall

写一个helloword程序,输入fmt后按C-xC-o如果能看到函数的声明展示出来,说明安装是正确的。

4、代码跳转提示godef

Set up godef

go get -v code.google点抗 /p/rog-go/exp/cmd/godef

go install -v code.google点抗 /p/rog-go/exp/cmd/godef

git clone ~/.vim/bundle/vim-godef

Configure vim in .vimrc file

Bundle 'dgryski/vim-godef'

Install Plugins

Launch vim and run

: PluginInstall

vim +PluginInstall +qall

5、代码结构提示gotags

Set up gotags

go get -u github点抗 /jstemmer/gotags

Put the following configuration in your vimrc:

Bundle 'majutsushi/tagbar'

nmap :TagbarToggle

let g:tagbar_type_go = {

\ 'ctagstype' : 'go',

\ 'kinds' : [

\ 'p:package',

\ 'i:imports:1',

\ 'c:constants',

\ 'v:variables',

\ 't:types',

\ 'n:interfaces',

\ 'w:fields',

\ 'e:embedded',

\ 'm:methods',

\ 'r:constructor',

\ 'f:functions'

\ ],

\ 'sro' : '.',

\ 'kind2scope' : {

\ 't' : 'ctype',

\ 'n' : 'ntype'

\ },

\ 'scope2kind' : {

\ 'ctype' : 't',

\ 'ntype' : 'n'

\ },

\ 'ctagsbin' : 'gotags',

\ 'ctagsargs' : '-sort -silent'

\ }

命令模式下按在右边就会显示当前文件下的函数名,结构体名等等,光标放到相应的tag上,按回车可以快速跳到程序中的相应位置。

再次按会关闭tag窗口。

PS:本地的.vimrc的配置

" 插件管理器 vundle

set nocompatible " be iMproved, required

filetype off " required

" set the runtime path to include Vundle and initialize

set rtp+=~/.vim/bundle/vundle/

call vundle#rc()

" alternatively, pass a path where Vundle should install plugins

"let path = '~/some/path/here'

"call vundle#rc(path)

" let Vundle manage Vundle, required

Plugin 'gmarik/vundle'

" The following are examples of different formats supported.

" Keep Plugin commands between here and filetype plugin indent on.

" scripts on GitHub repos

" Plugin 'tpope/vim-fugitive'

" Plugin 'Lokaltog/vim-easymotion'

" Plugin 'tpope/vim-rails.git'

" The sparkup vim script is in a subdirectory of this repo called vim.

" Pass the path to set the runtimepath properly.

" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}

" scripts from

" Plugin 'L9'

" Plugin 'FuzzyFinder'

" scripts not on GitHub

" Plugin 'git://git.wincent点抗 /command-t.git'

" git repos on your local machine (i.e. when working on your own plugin)

" Plugin ''

" ...

"

filetype plugin indent on " required

" To ignore plugin indent changes, instead use:

" filetype plugin on

"

" Brief help

" : PluginList - list configured plugins

" : PluginInstall(!) - install (update) plugins

" : PluginSearch(!) foo - search (or refresh cache first) for foo

" : PluginClean(!) - confirm (or auto-approve) removal of unused plugins

"

" see :h vundle for more details or wiki for FAQ

" NOTE: comments after Plugin commands are not allowed.

" Put your stuff after this line

syntax on

" ********************************************************************

" 这里省略了其它不相关的插件

" vimgdb插件

run macros/gdb_mappings.vim

" 官方的插件

" Some Linux distributions set filetype in /etc/vimrc.

" Clear filetype flags before changing runtimepath to force Vim to

" reload them.

filetype off

filetype plugin indent off

set runtimepath+=$GOROOT/misc/vim

filetype plugin indent on

syntax on

autocmd FileType go autocmd BufWritePre buffer Fmt

" 代码补全的插件

Bundle 'Blackrush/vim-gocode'

" 代码跳转提示

Bundle 'dgryski/vim-godef'

" 代码结构提示

Bundle 'majutsushi/tagbar'

nmap F8 :TagbarToggleCR

let g:tagbar_type_go = {

\ 'ctagstype' : 'go',

\ 'kinds' : [

\ 'p:package',

\ 'i:imports:1',

\ 'c:constants',

\ 'v:variables',

\ 't:types',

\ 'n:interfaces',

\ 'w:fields',

\ 'e:embedded',

\ 'm:methods',

\ 'r:constructor',

\ 'f:functions'

\ ],

\ 'sro' : '.',

\ 'kind2scope' : {

\ 't' : 'ctype',

\ 'n' : 'ntype'

\ },

\ 'scope2kind' : {

\ 'ctype' : 't',

\ 'ntype' : 'n'

\ },

\ 'ctagsbin' : 'gotags',

\ 'ctagsargs' : '-sort -silent'

\ }


当前文章:go语言godef go语言和python哪个好
网页URL:http://cdkjz.cn/article/ddpijce.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220