大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Go 由于不支持泛型而臭名昭著,但最近,泛型已接近成为现实。Go 团队实施了一个看起来比较稳定的设计草案,并且正以源到源翻译器原型的形式获得关注。本文讲述的是泛型的最新设计,以及如何自己尝试泛型。
创新互联是专业的浉河网站建设公司,浉河接单;提供网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行浉河网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
例子
FIFO Stack
假设你要创建一个先进先出堆栈。没有泛型,你可能会这样实现:
type Stack []interface{}func (s Stack) Peek() interface{} {
return s[len(s)-1]
}
func (s *Stack) Pop() {
*s = (*s)[:
len(*s)-1]
}
func (s *Stack) Push(value interface{}) {
*s =
append(*s, value)
}
但是,这里存在一个问题:每当你 Peek 项时,都必须使用类型断言将其从 interface{} 转换为你需要的类型。如果你的堆栈是 *MyObject 的堆栈,则意味着很多 s.Peek().(*MyObject)这样的代码。这不仅让人眼花缭乱,而且还可能引发错误。比如忘记 * 怎么办?或者如果您输入错误的类型怎么办?s.Push(MyObject{})` 可以顺利编译,而且你可能不会发现到自己的错误,直到它影响到你的整个服务为止。
通常,使用 interface{} 是相对危险的。使用更多受限制的类型总是更安全,因为可以在编译时而不是运行时发现问题。
泛型通过允许类型具有类型参数来解决此问题:
type Stack(type T) []Tfunc (s Stack(T)) Peek() T {
return s[len(s)-1]
}
func (s *Stack(T)) Pop() {
*s = (*s)[:
len(*s)-1]
}
func (s *Stack(T)) Push(value T) {
*s =
append(*s, value)
}
这会向 Stack 添加一个类型参数,从而完全不需要 interface{}。现在,当你使用 Peek() 时,返回的值已经是原始类型,并且没有机会返回错误的值类型。这种方式更安全,更容易使用。(译注:就是看起来更丑陋,^-^)
此外,泛型代码通常更易于编译器优化,从而获得更好的性能(以二进制大小为代价)。如果我们对上面的非泛型代码和泛型代码进行基准测试,我们可以看到区别:
type MyObject struct {
X
int
}
var sink MyObjectfunc BenchmarkGo1(b *testing.B) {
for i := 0; i b.N; i++ {
var s Stack
s.Push(MyObject{})
s.Push(MyObject{})
s.Pop()
sink = s.Peek().(MyObject)
}
}
func BenchmarkGo2(b *testing.B) {
for i := 0; i b.N; i++ {
var s Stack(MyObject)
s.Push(MyObject{})
s.Push(MyObject{})
s.Pop()
sink = s.Peek()
}
}
结果:
BenchmarkGo1BenchmarkGo1-16 12837528 87.0 ns/op 48 B/op 2 allocs/opBenchmarkGo2BenchmarkGo2-16 28406479 41.9 ns/op 24 B/op 2 allocs/op
在这种情况下,我们分配更少的内存,同时泛型的速度是非泛型的两倍。
合约(Contracts)
上面的堆栈示例适用于任何类型。但是,在许多情况下,你需要编写仅适用于具有某些特征的类型的代码。例如,你可能希望堆栈要求类型实现 String() 函数
Go语言里面定义变量有多种方式。
使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型放在变量名后面:
//定义一个名称为“variableName”,类型为"type"的变量
var variableName type
定义多个变量
//定义三个类型都是“type”的变量
var vname1, vname2, vname3 type
定义变量并初始化值
//初始化“variableName”的变量为“value”值,类型是“type”
var variableName type = value
C++用途广泛些。开始学是有点难,入门就好了。我从开始学到能看懂一般C++用了一个来月,或许你比我聪明用的更少。最后想说的就是要坚持,希望你成功。
1、Go(又称 Golang)是 Google 的 Robert Griesemer,Rob Pike 及 Ken Thompson 开发的一种静态强类型、编译型语言。Go 语言语法与 C 相近,但功能上有:内存安全,GC(垃圾回收),结构形态及 CSP-style 并发计算。
2、go:v.去;走;(尤指与某人)去(某处或出席某项活动);移动,旅行,行走(指方式或距离)。n.(游戏或活动中)轮到的机会;(做某事的)尝试,一番努力;精力;活力;热情;干劲。
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'
\ }
不下200种 可以在维基百科搜索List_of_programming_languages
百度贴不了wiki的地址,大概是垄断吧
A+BAT
A+
A++
A# .NET
A# (Axiom)
A-0
ABAP
ABC
ABC ALGOL
ABLE
ABSET
ABSYS
ACC
Accent
ActionScript
Ace DASL
ACT-III
Ada
APL
AWK
B
BACI
Baja
BASIC
bc
bcompile
BCPL
BeanShell
BETA
Bigwig
Big Snake
Bistro
BLISS
Blitz Basic
Block And List Manipulation (BALM)
Blue - Rejected prototype for Ada
Blue
Boo
Bourne shell - a.k.a sh
Bourne-Again shell - see Bash
Boxx
BPEL - Business Process Execution Language
Brainfuck
BUGSYS
BuildProfessional
BYOND
C
C--
C-script
C++ - ISO/IEC 14882
C# - ISO/IEC 23270
C shell (csh)
Caché ObjectScript - See also Caché Basic
Caml
Cat
Cayenne
C-BOT
Cecil
Cesil
Cg
Ch interpreter (C/C++ interpreter Ch)
Chapel
CHAIN
Charity
Chef
Chey
CHILL
CHIP-8
chomski
Chrome
ChucK
Cilk
CICS
CL
Clarion
Clean
Clipper
CLIST - Programming language for online applications in the MVS TSO environment
CLU
CMS-2
COBOL - ISO/IEC 1989
CobolScript
Cobra
CODE
ColdFusion
COMAL
Common Intermediate Language (CIL)
Common Lisp
Component Pascal
COMIT - List or string processing language
Concurrent Clean
Constraint Handling Rules
CORAL66
Corn
CorVision
COWSEL
CPL
CSP
Csound
Cue
Curl
Curry
Cyclone
D
Dao
DASL - Distributed Application Specification Language
DASL - Datapoint's Advanced Systems Language
DarkBASIC
DarkBASIC Professional
Dataflex
Datalog
dBASE
dc
Deesel (formerly G)
Delphi
Dialect
DinkC
DCL
Dialog Manager
DIBOL
DL/I
Dream Maker
Dylan
Dynace
E
Ease
EASY
Easy PL/I
EASYTRIEVE PLUS
eC (Ecere C)
ECMAScript
eDeveloper
Edinburgh IMP
Einstein
Eiffel
Elan
elastiC
Elf
Emacs Lisp
EGL Programming Language (EGL)
Epigram
Erlang
Escapade - server-side programming
Esterel
Euclid
Euphoria
Euler
EXEC
EXEC2
F
F#
Factor
Fan
Felix
Ferite
F#
FL
FLOW-MATIC
FOCAL
FOCUS
FOIL
FORMAC
Formula language
Forth
Fortran - ISO/IEC 1539
Fortress
FoxPro
FP
Frag Script
Franz Lisp
Frink
Frontier
F-Script
Gambas
G-code
General Algebraic Modeling System
Generic Java
Gibiane
G (LabVIEW)
Gödel
Godiva
GOTRAN (see IBM 1620)
GOTO++
GPSS
GraphTalk
GRASS
Green
Groovy
H - Business processing language from NCR.
HAL/S - Real-time aerospace programming language
HAScript
Haskell - An advanced functional programming language
HaXe - Open Source language which can compile to four different platforms, including PHP and Flash
HyperTalk
IBM Basic assembly language
IBM RPG
ICI
Icon
IDL
IMP
Inform
Information Processing Language (IPL)
Informix-4GL
Io
IPTSCRAE
Interactive System Productivity Facility
J
J#
J++
JADE
JAG
Jal
Janus
Java
JavaScript
Jim++
JCL
Join Java
JOSS
Joule
JOVIAL
Joy
JScript
JSP
J2EE
J2ME
K
KEE
Kiev
Korn Shell
KIF
Kite
Kogut
KRC
KRL
KRYPTON
L
LabVIEW
Lagoona
LANSA
Lasso
Lava
Leda
Lead
Leadwerks Script
Legoscript
Leopard
Lexico
Lfyre
Liberty BASIC
Limbo
Limnor
LINC
Lingo
Lisaac
Lisp - ISO/IEC 13816
Lite-C
Logo
LOLCODE
LPC
LSL
LSE
Lua
Lucid
Lush
Lustre
LYaPAS
LSL
M4
MAD
MADCAP
MAGIC - See eDeveloper
Magik
Magma
MapBasic
Maple
MAPPER (Unisys/Sperry) now part of BIS
M-A-R-E-K (Programming language)
MARK-IV (Sterling/Informatics) now VISION:BUILDER of CA
Mary
Mathematica
MATLAB
MATA
Maxima (see also Macsyma)
MaxScript internal language 3D Studio Max
Maya (MEL)
Multiprocessor C#
Mercury
Mesa
METAL
Michigan Algorithm Decoder see MAD programming language
Microcode
MicroScript
MillScript
MIMIC
Mindscript
Miranda
Miva
ML
Moby
MODCAP
Model 204 User Language
Modula
Modula-2
Modula-3
Mondrian
Mortran
Moto
MOUSE
MSIL - Deprecated name for Common Intermediate Language
MSL
MONO
MUMPS
Napier88
Natural
Nemerle
NESL
Net.Data
Neuralware
NewtonScript
NGL
Nial
NXT-G
Nice
Nickle
Nosica
NQC
Nu
o:XML
Oberon
Objective Modula-2
Object Lisp
ObjectLOGO
Object Pascal
Objective-C
Objective Caml
Obliq
Objectstar
ObjectView
Ocaml
occam
occam-π
Octave
OmniMark
Opal
Open programming language
OPS5
Organiser Programming Language (OPL) - cf. Psion Organiser
Oxygene
Oz
PARI/GP
Parser
Pascal - ISO 7185
Pawn
PBASIC
PCASTL
PEARL
Perl
Perl Data Language
PHP
Pico
Piet
Pike
PIKT
PILOT
Pizza
PL 11
PL/0
PL/8
PL/B
PL/C
PL/I - ISO 6160
PL/M
PL/P
PL/SQL
Plankalkül
PLD
PLEX
PLEXIL
Pliant
PNGlish
PPL
POP-11
Poplog
PORTRAN
PostScript
Ppc++
Processing
Prograph
Progress 4GL
Prolog
Turbo Prolog
Promela
Protheus
PRO-IV
Python
Q
Qi
QtScript
QuakeC
QPL
Quikcomp (for the Moonrobot XI)
R
R++ - Based on C++ and added semanteme description
Rascal
Ratfiv
Ratfor
RBScript
rc
REPL - Really Easy Programming Language
REBOL - Relative Expression Based Object Language
Red - Rejected prototype for Ada
Redcode
REDO
REFAL
Revolution
REXX
Rigal
Rlab
Robot Scripting Language (RSL)
RPG - Report Program Generator
RPL
RScript
Ruby
Russell Programming Language
REALBasic
S
S2
S-PLUS
S-Lang
SAIL
SAKO
SAM76
SAS
Sather
Scala
ScalPL
SCAR
SCATRAN
Scheme
Scilab
Script.NET
Sed
Seed7
Self
SETL
Shadow Programming Interface (Developing)
ShadowScript
Shift Script
SIGNAL
SiMPLE
SIMPOL
SIMSCRIPT
Simula
SISAL
Slate
SLIP
SMALL - SMALL Machine Algol Like Language
Small
Smalltalk
SNOBOL - String Oriented Symbolic Language
SPITBOL
Snowball
SPARK
Spice
SPIN
SP/k
SPL/1 - aka SPL/I
SPS (1620) - see IBM 1620
Squirrel
SR
SSL
Standard ML
StringLang
Subtext
SuperCollider
Suneido
SYMPL
SyncCharts
Synergy/DE
SystemVerilog
T
TACL
TACPOL
TagsMe
TADS
TIE
Transaction Application Language
Tcl
Transact-SQL
teco
TELCOMP
Telon
Tempo
thinBasic
Titanium
TI-Basic
Today
Tom
TOM
Topspeed - see Clarion
TorqueScript
tpu
Trac
Trine
TTCN
Turbo Pascal
Turing
TUTOR\
Tutorial D
TXL
Ubercode
Ultra 32
Unicon
Uniface
Unix shell
Unlambda
UnrealScript
Use
V
Vala
VDM++
VDM-SL
Verilog
VHDL
Visual Assembler
Visual Basic - Visual Beginners All-purpose Symbolic Instruction Code
Visual Basic .NET
Visual DataFlex
Visual DialogScript
Visual FoxPro
Visual J++
Visual Objects
VBScript
VX-REXX
Vvvv
Water
WATFOR - see WATFIV
WATFIV
WAXHTML
WebQL
Whitespace
Winbatch
WinDev
Windows PowerShell
X++
X10
Xbase++ 32Bit Windows language
XBL
xbScript - Also xBaseScript
xHarbour
XL
XOTcl
XPL
XPL0
XQuery
XSLT - See XPath
Y
YACC
YAFL
Yellow - Rejected prototype for Ada
Yorick
Y
Z++
Z notation - A program specification language, like UML.
Zonnon
ZOPL
ZPL