135-1821-9792

go语言gtk环境搭建 go语言开发环境搭建

如何配置go语言开发环境

1、下载go的zip文件。并且一定要把文件解压到c:\go目录下。

成都创新互联公司是一家网站设计公司,集创意、互联网应用、软件技术为一体的创意网站建设服务商,主营产品:成都响应式网站建设公司品牌网站建设成都全网营销。我们专注企业品牌在网站中的整体树立,网络互动的体验,以及在手机等移动端的优质呈现。成都网站建设、做网站、移动互联产品、网络运营、VI设计、云产品.运维为核心业务。为用户提供一站式解决方案,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏析悦目的作品,网站的价值服务。

2、配置windows的高级环境变量。包括:GOROOT、GOOS、GOBIN、GOARCH。并且在path变量里面把c:\go\bin加入。以便可以在命令行直接运行go命令。

举例:我的机器:

GOPATH= c:\go;c:\go\src;F:\workspace\goSample01;

GOBIN=c:\go\bin;F:\workspace\goSample01\bin;

其中,c:\go是go的安装路径;

F:\workspace\goSample01是我写的go语言项目的工程目录;

F:\workspace\goSample01\bin是go语言项目的工程目录下的可执行文件路径;

3、在完成环境变量配置后,打开一个命令行窗口,直接输入go,然后回车,看看是否出现go的帮助信息。如果出现,那么go的基本环境就OK了。

注意:这个基本环境不包含开发工具,也不能直接编译带C代码的go程序。

4、

(可选)为了支持Import远程包,最好装个gomingw。下载地址:

/downloads/list。如果下的是压缩包,请把它解压到C盘。例如,C:\gowin-env。里面有个Console.bat是以后使用go

get的环境。举例:有个文件a.go,里面import(

"fmt"

"github点抗 /astaxie/beedb"

_ "github点抗 /ziutek/mymysql/godrv"

为了编译该a.go文件,需要启动Console.bat,然后在该命令行窗口,进入c:\go\src目录下,执行go getgithub点抗 /astaxie/beedb

Go get github点抗 /ziutek/mymysql/godrv .

Go会自动下载该远程包并编译和安装这些包。

配置goclipse(可选)

(如果不喜欢eclipse开发工具,请跳过这个配置。)

1、下载并安装goclipse插件。Goclipse是go语言for eclipse的插件,下载地址:

2、启动eclipse并创建go项目。然后写个最简单的helloworld.go文件,并运行。代码如下:

packagemainimport"fmt"func main(){ fmt.Printf("hello, world")}

配置gocode(可选)

如果不需要go语法辅助和eclipse里面的(按ALT+/)弹出go语言自动辅助功能,请跳过这个配置。

1、下载gocode的zip文件,解压后放在go的bin目录下。

2、下载并安装Git软件。并且在path里面配置git的执行路径。例如c:\git\bin

3、在命令行执行:go build .\gocode。如果一切正常,那么将会编译生成一个gocode.exe文件在go的bin目录下。如果编译失败,那么就转第4步。

4、如果第3步直接编译gocode源文件成功,那就直接到第5步。否则,就需要通过git下载gocode源文件,然后再编译。在命令行执行:go get -u github点抗 /nsf/gocode 。就会生成gocode.exe文件。

5、在goclipse插件里面指定gocode的路径。就可以在elcipse里面调用gocode来帮助写编码了。

从开发工具这块看,go语言还不够成熟,开发工具都还不完善,有待改进。

下载go-tour教程源代码(可选)

Google有个在线运行go语言的教程(),很不错。支持在web上直接运行大部分的go程序,想了解这个教程的源代码的朋友可以通过以下方式获取。如果没兴趣,可以跳过这个步骤。

1、下载安装Mercurial软件。

2、在命令行下输入:

hg clone

作为测试用的。如果把http改成https协议,下载就会失败。搞不懂。

编译带调用C代码的go文件(可选)

1、为了在windows下编译带C代码的go程序,你首先需要下载并安装MinGW或者Cygwin。

2、首选安装MinGW。在安装MinGW之后,记得要把MinGW安装目录\bin路径设置在path环境变量里面,以便能在dos窗口下直接调用gcc。

3、下载一个gowin-env。下载地址:gowin-env。下载后解压到某个目录下,例如:C:\gowin-env. 然后,编辑go-env.bat。配置相关的go参数。例如,我的配置是:

set GOARCH=386

set GOOS=windows

set GOROOT=c:\go

set GOBIN=%GOROOT%\bin

set GOPATH=%GOROOT%;F:\workspace\goSample01;

设置好go-env.bat后,就可以点击Console.bat来启动编译和运行窗口。

4、编写一个带C代码的go程序。例如,testc.go

5、编译

例如:

go build -compiler gccgo test_c.go

运行调用C代码的go文件(可选)

1、testc.go.

创建rand目录,然后在rand里面创建testc.go. 代码如下:

package rand

/*

//

#include stdio.h

*/

import "C"

func PrintHello() {

C.puts(C.CString("Hello, world\n"))

}

2、a.go

在rand下创建a.go.代码如下:

package rand

import "fmt"

func SayHello(name string){

fmt.Println(name)

}

3、test_import.go

在rand的上一级创建test_import.go。代码如下:

package main

import "./rand"

func main(){

rand.SayHello("tom")

rand.PrintHello()

}

4、运行test_import.go

go run test_import.go

在测试其它几个C代码的时候,发现windows版本的cgo还有些编译问题,同样的代码转移到苹果的XCODE下就没有问题。后来终于发现原因了,原来有些例子是unix平台下的,而在windows平台下,方法名和参数需要做调整。

例如:下面代码在windows下编译报一堆错误。

package rand

/*

#include stdlib.h

*/

import "C"

func Random() int {

return int(C.random())

}

func Seed(i int) {

C.srandom(C.uint(i))

}

这里需要把return int(C.random()) 修改为“return int(C.rand())”

C.srandom(C.uint(i))修改为“C.srand(C.uint(i))”编译就OK了。

如何搭建go语言环境 linux

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用户来说当然是写入环境变量。

如何配置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'

\ }

如何在win7环境下搭建Go开发环境

首先需要下载下载go平台安装包

安装程序 下载地址址h如果是您的系统是windows32 位系统请选择go1.3.3.windows-386.msi即可,其他的,请按照自己所需版本来进行下载,如下图所示:

安装以及配置环境变量

由于Windows下的的安装包有两种:msi和zip的;zip的是免安装的,解压在配置一些环境变量之后就可以使用,msi的则是安装包版本的,安装的时候会设置好对应的环境变量。我的电脑是Win7 64位,因为方便,就下载了下面的版本。下好安装包后,安装过程就很简单了,一路“Next”就好了。 虽然msi会自动配置一些环境变量,但是本人在安装完之后还是自己配置了环境变量,所以在安装完Go之后,我们最好还是检查一些所有的环境变量是否正常。主要的环境变量有以下几个:

GOROOT:Go的安装目录

GOPATH:用于存放Go语言Package的目录,这个目录不能在Go的安装目录中

GOBIN:Go二进制文件存放目录,写成%GOROOT%\bin就好

GOOS:操作系统

GOARCH:指定系统环境,i386表示x86,amd64表示x64

PATH:需要将%GOBIN%加在PATH变量的最后,方便在命令行下运行Go

如下图所示:

像我自己安装的,下载完成之后解压到任意目录(所有目录均不能使用中文):D:\Go;

然后是go环境变量的配置:

GOARCH:386(go安装版本)

GOBIN:D:\Go\bin(exe执行文件路径)

GOOS:windows(go运行的系统)

GOROOT:D:\Go(go的解压路径)

GOPATH:E:\go\data(go的工具包路径,随意指定,后面会用到)

然后在path环境变量中追加:;%GOBIN%

完成之后在cmd窗口输入:go version,如下图所示:


本文题目:go语言gtk环境搭建 go语言开发环境搭建
分享链接:http://kswsj.com/article/ddicejo.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号