go语言开发的网站环境 go语言开发的网站环境怎么样

go语言的webengine叫什么Java教程
Linux入门
更多
首页
?
Go语言WEB框架(Gin)详解
在 Go语言开发的 Web 框架中,有两款著名 Web 框架分别是 Martini 和 Gin,两款 Web 框架相比较的话,Gin 自己说它比 Martini 要强很多 。
Gin 是 Go语言写的一个 web 框架 , 它具有运行速度快 , 分组的路由器,良好的崩溃捕获和错误处理,非常好的支持中间件和 json 。总之在 Go语言开发领域是一款值得好好研究的 Web 框架,开源网址:
首先下载安装 gin 包:
go get -u github.com/gin-gonic/gin
一个简单的例子:
package main
import "github.com/gin-gonic/gin"
func main() {
//Default返回一个默认的路由引擎
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
//输出json结果给调用方
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
编译运行程序,打开浏览器,访问页面显示:
{"message":"pong"}
gin 的功能不只是简单输出 Json 数据 。它是一个轻量级的 WEB 框架,支持 RestFull 风格 API,支持 GET,POST,PUT,PATCH,DELETE,OPTIONS 等 http 方法,支持文件上传 , 分组路由,Multipart/Urlencoded FORM , 以及支持 JsonP,参数处理等等功能,这些都和 WEB 紧密相关,通过提供这些功能,使开发人员更方便地处理 WEB 业务 。
Gin 实际应用
接下来使用 Gin 作为框架来搭建一个拥有静态资源站点,动态 WEB 站点,以及 RESTFull API 接口站点(可专门作为手机 APP 应用提供服务使用)组成的,亦可根据情况分拆这套系统,每种功能独立出来单独提供服务 。
下面按照一套系统但采用分站点来说明,首先是整个系统的目录结构,website 目录下面 static 是资源类文件 , 为静态资源站点专用;photo 目录是 UGC 上传图片目录,tpl 是动态站点的模板 。
当然这个目录结构是一种约定,可以根据情况来修改 。整个项目已经开源,可以访问来详细了解:具体每个站点的功能怎么实现呢?请看下面有关每个功能的讲述:
静态资源站点
一般网站开发中,我们会考虑把 js , css,以及资源图片放在一起,作为静态站点部署在 CDN , 提升响应速度 。采用 Gin 实现起来非常简单,当然也可以使用 net/http 包轻松实现,但使用 Gin 会更方便 。
不管怎么样,使用 Go 开发,我们可以不用花太多时间在 WEB 服务环境搭建上,程序启动就直接可以提供 WEB 服务了 。
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// 静态资源加载,本例为css,js以及资源图片
router.StaticFS("/public", http.Dir("D:/goproject/src/github.com/ffhelicopter/tmm/website/static"))
router.StaticFile("/favicon.ico", "./resources/favicon.ico")
// Listen and serve on 0.0.0.0:80
router.Run(":80")
}
首先需要是生成一个 Engine , 这是 gin 的核心,默认带有 Logger 和 Recovery 两个中间件 。
router := gin.Default()
StaticFile 是加载单个文件 , 而 StaticFS 是加载一个完整的目录资源:
func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes
这些目录下资源是可以随时更新,而不用重新启动程序 。现在编译运行程序 , 静态站点就可以正常访问了 。
如何配置go语言集成开发环境 vim1、编译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 -p0vimgdb-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
vimPluginInstallqall
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 BufWritePreFmt
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
vimPluginInstallqall
写一个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
vimPluginInstallqall
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'
\ }
phpstorm golang开发环境如何配置golang 有编辑器可以用 如国人开发的liteide,或者sublimetext、vim、emacs,为什么一定要用phpstorm?如果你只是做php开发,phpstorm很不错,如果用golang来开发,phpstorm 并不合适
如何配置go语言开发环境1.1 Go 安装
Go的三种安装方式
Go有多种安装方式,你可以选择自己喜欢的 。这里我们介绍三种最常见的安装方式:
Go源码安装:这是一种标准的软件安装方式 。对于经常使用Unix类系统的用户,尤其对于开发者来说,从源码安装可以自己定制 。
Go标准包安装:Go提供了方便的安装包 , 支持Windows、Linux、Mac等系统 。这种方式适合快速安装,可根据自己的系统位数下载好相应的安装包 , 一路next就可以轻松安装了 。**推荐这种方式**
第三方工具安装:目前有很多方便的第三方软件包工具 , 例如Ubuntu的apt-get、Mac的homebrew等 。这种安装方式适合那些熟悉相应系统的用户 。
最后,如果你想在同一个系统中安装多个版本的Go , 你可以参考第三方工具GVM,这是目前在这方面做得最好的工具,除非你知道怎么处理 。
Go源码安装
在Go的源代码中,有些部分是用Plan 9 C和ATT汇编写的,因此假如你要想从源码安装,就必须安装C的编译工具 。
在Mac系统中,只要你安装了Xcode,就已经包含了相应的编译工具 。
在类Unix系统中 , 需要安装gcc等工具 。例如Ubuntu系统可通过在终端中执行sudo apt-get install gcc
libc6-dev来安装编译工具 。
在Windows系统中,你需要安装MinGW,然后通过MinGW安装gcc,并设置相应的环境变量 。
你可以直接去官网下载源码 , 找相应的goVERSION.src.tar.gz的文件下载,下载之后解压缩到$HOME目录,执行如下代码:
cd go/src
./all.bash
运行all.bash后出现"ALL TESTS PASSED"字样时才算安装成功 。
上面是Unix风格的命令,Windows下的安装方式类似,只不过是运行all.bat,调用的编译器是MinGW的gcc 。
如果是Mac或者Unix用户需要设置几个环境变量 , 如果想重启之后也能生效的话把下面的命令写到.bashrc或者.zshrc里面,
export GOPATH=$HOME/gopath
export PATH=$PATH:$HOME/go/bin:$GOPATH/bin
如果你是写入文件的,记得执行bash .bashrc或者bash
.zshrc使得设置立马生效 。
如果是window系统 , 就需要设置环境变量,在path里面增加相应的go所在的目录,设置gopath变量 。
当你设置完毕之后在命令行里面输入go,看到如下图片即说明你已经安装成功
图1.1 源码安装之后执行Go命令的图
如果出现Go的Usage信息 , 那么说明Go已经安装成功了;如果出现该命令不存在,那么可以检查一下自己的PATH环境变中是否包含了Go的安装目录 。
关于上面的GOPATH将在下面小节详细讲解
Go标准包安装
Go提供了每个平台打好包的一键安装,这些包默认会安装到如下目录:/usr/local/go
(Windows系统:c:\Go),当然你可以改变他们的安装位置,但是改变之后你必须在你的环境变量中设置如下信息:
export GOROOT=$HOME/go
export GOPATH=$HOME/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
上面这些命令对于Mac和Unix用户来说最好是写入.bashrc或者.zshrc文件,对于windows用户来说当然是写入环境变量 。
如何判断自己的操作系统是32位还是64位?
我们接下来的Go安装需要判断操作系统的位数,所以这小节我们先确定自己的系统类型 。
Windows系统用户请按Win R运行cmd,输入systeminfo后回车,稍等片刻,会出现一些系统信息 。在“系统类型”一行中,若显示“x64-based
PC”,即为64位系统;若显示“X86-based PC”,则为32位系统 。
Mac系统用户建议直接使用64位的 , 因为Go所支持的Mac OS X版本已经不支持纯32位处理器了 。
Linux系统用户可通过在Terminal中执行命令arch(即uname
-m)来查看系统信息:
64位系统显示
x86_64
32位系统显示
i386
Mac 安装
访问下载地址,32位系统下载go1.4.2.darwin-386-osx10.8.pkg,64位系统下载go1.4.2.darwin-amd64-osx10.8.pkg , 双击下载文件,一路默认安装点击下一步,这个时候go已经安装到你的系统中 , 默认已经在PATH中增加了相应的~/go/bin,这个时候打开终端 , 输入go
看到类似上面源码安装成功的图片说明已经安装成功
如果出现go的Usage信息,那么说明go已经安装成功了;如果出现该命令不存在,那么可以检查一下自己的PATH环境变中是否包含了go的安装目录 。
Linux 安装
访问下载地址,32位系统下载go1.4.2.linux-386.tar.gz,64位系统下载go1.4.2.linux-amd64.tar.gz,
假定你想要安装Go的目录为 $GO_INSTALL_DIR , 后面替换为相应的目录路径 。
解压缩tar.gz包到安装目录下:tar zxvf go1.4.2.linux-amd64.tar.gz -C
$GO_INSTALL_DIR 。
设置PATH,export PATH=$PATH:$GO_INSTALL_DIR/go/bin
然后执行go
图1.2 Linux系统下安装成功之后执行go显示的信息
如果出现go的Usage信息,那么说明go已经安装成功了;如果出现该命令不存在,那么可以检查一下自己的PATH环境变中是否包含了go的安装目录 。
Windows 安装
访问Google Code 下载页,32
位请选择名称中包含 windows-386 的 msi 安装包,64 位请选择名称中包含 windows-amd64 的 。下载好后运行,不要修改默认安装目录
C:\Go\,若安装到其他位置会导致不能执行自己所编写的 Go 代码 。安装完成后默认会在环境变量 Path 后添加 Go 安装目录下的 bin 目录
C:\Go\bin\,并添加环境变量 GOROOT,值为 Go 安装根目录 C:\Go\。
验证是否安装成功
在运行中输入 cmd 打开命令行工具,在提示符下输入 go,检查是否能看到 Usage 信息 。输入
cd %GOROOT%,看是否能进入 Go 安装目录 。若都成功,说明安装成功 。
不能的话请检查上述环境变量 Path 和 GOROOT 的值 。若不存在请卸载后重新安装,存在请重启计算机后重试以上步骤 。
第三方工具安装
GVM
gvm是第三方开发的Go多版本管理工具,类似ruby里面的rvm工具 。使用起来相当的方便 , 安装gvm使用如下命令:
bash(curl -s -S -L )
安装完成后我们就可以安装go了:
gvm install go1.4.2
gvm use go1.4.2
也可以使用下面的命令,省去每次调用gvm use的麻烦: gvm use go1.4.2 --default
执行完上面的命令之后GOPATH、GOROOT等环境变量会自动设置好 , 这样就可以直接使用了 。
apt-get
Ubuntu是目前使用最多的Linux桌面系统,使用apt-get命令来管理软件包 , 我们可以通过下面的命令来安装Go , 为了以后方便,应该把
git mercurial 也安装上:
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:gophers/go
sudo apt-get update
sudo apt-get install golang-stable git-core mercurial
homebrew
homebrew是Mac系统下面目前使用最多的管理软件的工具,目前已支持Go,可以通过命令直接安装Go,为了以后方便 , 应该把
git mercurial 也安装上:
brew updatebrew upgrade
brew install go
brew install git
brew install mercurial
Go语言的支持平台 LiteIDE是一款专门为Go语言开发的跨平台轻量级集成开发环境(IDE),由QT编写 。
LiteIDE主要特点:支持主流操作系统
Windows
Linux
MacOS XGo编译环境管理和切换
管理和切换多个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)GoSublimegocodeMarGo的组合 。
其优点有:自动化提示代码 。保存的时候自动格式化代码,让您编写的代码更加美观,符合Go的标准 。支持项目管理支持语法高亮熟悉Java的读者应该对于idea不陌生 , idea是通过一个插件来支持go语言的高亮语法,代码提示和重构实现 。
【go语言开发的网站环境 go语言开发的网站环境怎么样】go语言开发的网站环境的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于go语言开发的网站环境怎么样、go语言开发的网站环境的信息别忘了在本站进行查找喔 。

    推荐阅读