资讯

精准传达 • 有效沟通

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

go语言页面跳转 go语言怎么运行

怎么学习golang

除了Java、Python和JavaScript之外,如果要开始学习一门新语言的话,我想应该是Go!

创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的双辽网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

Go语言正在被越来越多的公司使用。我们公司的后端服务已经全面采用Go语言实现了。

最开始接触Go语言是去年将一份Go代码“翻译”成Python并集成到测试平台上,说来也挺神奇,我从来没学过Go却完成了这个工作,这也侧面反应了Go的语法还是很平易近人的。

今年,在海翔飞调岗之后已经没有太多时间写代码了,但如果要开始学习一个新的语言或技术的话,我最想学的是Go!

目前来看,Go似乎还并没有太多测试人员使用的场景,不过,我之前介绍过的BDD行为驱动框架gauge是由Go开发的,当然,它也支持使用Go来编写BDD测试代码。

对于,已经有一定开发经验的同学,如何快速的开始学习Go语言呢?我这里给一些思路。

#### 第一步:下载和安装

在配置环境的时候你需要重点了解GOROOT、GOPATH的作用。

你还要准备一款称手的编辑器,如果你像我一样,一直都在使用VS Code的话,那么就它就可以了。

#### 第二步:从hello world开始

先运行一个hello world程序,认识Go语言的语法。

package main

import (    "fmt")

func main(){

fmt.Println("helloworld!")

}

#### 第三步:熟悉Go的语法

接下来,你可能要花一周左右的时间熟悉Go语言的语法。比如,变量定义、if/for、函数、Map、跨文件的程序调用…等,当然,还有一些Go特有的知识。

当然,我更喜欢看视频教程,虽然质量参差不齐,但我仍然觉得看视频比我自己看书更有效率。

熟悉一段Go代码:

package main

import"fmt"

func myFunc() {

i := 0

Here:   //这行的第一个词,以冒号结束作为标签

fmt.Println(i)

i++    if i 10{        goto Here   //跳转到Here去

}

}func main() {    //调用函数

myFunc()

}

#### 第四步:Go如何做单元测试

针对Go做测试也非常简单。比如,这是一个被测试文件:add.go。

package test_demofunc Add(a int, b int) int{    return a + b

}

下面针对Add()函数编写测试用例,test_add.go

package test_demo

import (    "testing")

func TestAdd1(t *testing.T){

r:= Add(1, 2)    if r !=3{

t.Errorf("Add(1, 2)failed. Got %d, expected 3.", r)

}

}

func TestAdd2(t *testing.T){

r:= Add(2, 2)    if r !=4{

t.Errorf("Add(2, 2)failed. Got %d, expected 4.", r)

}

}

你只需要执行 go test 命令就可以运行上面的测试了。

#### 第五步:从哪儿找第三方库

当然,你只学习go语言本身,基本是做不了什么事的,必须要使用第三方扩展库。

这里罗列了Go语言的第三方库,通过这些第三方库的介绍,我们也可以大概知道Go可以用来干什么。

如果你知道库的名字的话,也可以在这个网站上搜索。

据我了解,Go的第三方库大多都在GitHub上面。

#### 第六步:用Go做Web开发

Go是静态语言,而且支持并发编程,所以,他有天然的性能优势,大多公司主要使用Go也是开发后端服务(即API)。

终于到了实战阶段,如果我们真的要掌握一门语言,那么一定要用它来开发一个项目出来。这个过程大概需要一个月。

Beego是Go下在主流的Web开发框架,资料相对比较丰富,而且有完善的文档。你可为此制定一个目标,比如用它来开发一个Blog,为此,你需要详细阅读Beego文档,以及学习相关的Web开发技术。

等你完成这个项目的时候,我想你已经会使用Go语言了。

Go语言的支持平台

LiteIDE是一款专门为Go语言开发的跨平台轻量级集成开发环境(IDE),由QT编写。

LiteIDE主要特点: 支持主流操作系统

Windows

Linux

MacOS X Go编译环境管理和切换

管理和切换多个Go编译环境

支持Go语言交叉编译 与Go标准一致的项目管理方式

基于GOPATH的包浏览器

基于GOPATH的编译系统

基于GOPATH的Api文档检索 Go语言的编辑支持

类浏览器和大纲显示

Gocode(代码自动完成工具)的完美支持

Go语言文档查看和Api快速检索

代码表达式信息显示F1

源代码定义跳转支持F2

Gdb断点和调试支持

gofmt自动格式化支持 其他特征

支持多国语言界面显示

完全插件体系结构

支持编辑器配色方案

基于Kate的语法显示支持

基于全文的单词自动完成

支持键盘快捷键绑定方案

Markdown文档编辑支持

实时预览和同步显示

自定义CSS显示

可导出HTML和PDF文档

批量转换/合并为HTML/PDF文档 Sublime Text 2(以下简称Sublime)+ GoSublime + gocode + MarGo的组合。

其优点有: 自动化提示代码。 保存的时候自动格式化代码,让您编写的代码更加美观,符合Go的标准。 支持项目管理 支持语法高亮 熟悉Java的读者应该对于idea不陌生,idea是通过一个插件来支持go语言的高亮语法,代码提示和重构实现。

Go语言和其他语言的不同之基本语法

Go语言作为出现比较晚的一门编程语言,在其原生支持高并发、云原生等领域的优秀表现,像目前比较流行的容器编排技术Kubernetes、容器技术Docker都是用Go语言写的,像Java等其他面向对象的语言,虽然也能做云原生相关的开发,但是支持的程度远没有Go语言高,凭借其语言特性和简单的编程方式,弥补了其他编程语言一定程度上的不足,一度成为一个热门的编程语言。

最近在学习Go语言,我之前使用过C#、Java等面向对象编程的语言,发现其中有很多的编程方式和其他语言有区别的地方,好记性不如烂笔头,总结一下,和其他语言做个对比。这里只总结差异的地方,具体的语法不做详细的介绍。

种一棵树最好的时间是十年前,其次是现在。

3)变量初始化时候可以和其他语言一样直接在变量后面加等号,等号后面为要初始化的值,也可以使用变量名:=变量值的简单方式

3)变量赋值 Go语言的变量赋值和多数语言一致,但是Go语言提供了多重赋值的功能,比如下面这个交换i、j变量的语句:

在不支持多重赋值的语言中,交换两个变量的值需要引入一个中间变量:

4)匿名变量

在使用其他语言时,有时候要获取一个值,却因为该函数返回多个值而不得不定义很多没有的变量,Go语言可以借助多重返回值和匿名变量来避免这种写法,使代码看起来更优雅。

假如GetName()函数返回3个值,分别是firstName,lastName和nickName

若指向获得nickName,则函数调用可以这样写

这种写法可以让代码更清晰,从而大幅降低沟通的复杂度和维护的难度。

1)基本常量

常量使用关键字const 定义,可以限定常量类型,但不是必须的,如果没有定义常量的类型,是无类型常量

2)预定义常量

Go语言预定义了这些常量 true、false和iota

iota比较特殊,可以被任务是一个可被编译器修改的常量,在每个const关键字出现时被重置为0,然后在下一个const出现之前每出现一个iota,其所代表的数字会自动加1.

3)枚举

1)int 和int32在Go语言中被认为是两种不同类型的类型

2)Go语言定义了两个浮点型float32和float64,其中前者等价于C语言的float类型,后者等价于C语言的double类型

3)go语言支持复数类型

复数实际上是由两个实数(在计算机中使用浮点数表示)构成,一个表示实部(real)、一个表示虚部(imag)。也就是数学上的那个复数

复数的表示

实部与虚部

对于一个复数z=complex(x,y),就可以通过Go语言内置函数real(z)获得该复数的实部,也就是x,通过imag(z)获得该复数的虚部,也就是y

4)数组(值类型,长度在定义后无法再次修改,每次传递都将产生一个副本。)

5)数组切片(slice)

数组切片(slice)弥补了数组的不足,其数据结构可以抽象为以下三个变量:

6)Map 在go语言中Map不需要引入任何库,使用很方便

Go循环语句只支持for关键字,不支持while和do-while

goto语句的语义非常简单,就是跳转到本函数内的某个标签

今天就介绍到这里,以后我会在总结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.com/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.com/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.com/p/rog-go/exp/cmd/godef

go install -v code.google.com/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.com/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.com/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语言页面跳转 go语言怎么运行
分享网址:http://cdkjz.cn/article/ddddoso.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220