GO 使用开源的数据结构

痞子汤 · · 1462 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

1.  平台 Windows, 安装 git

2. 安装 go 1.3+

3. 查看 GOPATH 信息

C:\>go env
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Administrator\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

4. 下载代码,测试代码

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\Administrator>cd C:\Users\Administrator\go

C:\Users\Administrator\go>go get github.com/Workiva/go-datastructures/...

C:\Users\Administrator\go>go get -u github.com/Workiva/go-datastructures/...

C:\Users\Administrator\go>cd src

C:\Users\Administrator\go\src>cd github.com

C:\Users\Administrator\go\src\github.com>cd Workiva

C:\Users\Administrator\go\src\github.com\Workiva>cd go-datastructures

C:\Users\Administrator\go\src\github.com\Workiva\go-datastructures>go get -t -u
./...

C:\Users\Administrator\go\src\github.com\Workiva\go-datastructures>go test ./...

?       github.com/Workiva/go-datastructures    [no test files]
ok      github.com/Workiva/go-datastructures/augmentedtree      2.126s
ok      github.com/Workiva/go-datastructures/batcher    1.363s
ok      github.com/Workiva/go-datastructures/bitarray   1.581s
ok      github.com/Workiva/go-datastructures/btree/immutable    15.153s
ok      github.com/Workiva/go-datastructures/btree/palm 1.064s
ok      github.com/Workiva/go-datastructures/btree/plus 1.783s
ok      github.com/Workiva/go-datastructures/cache      0.820s
?       github.com/Workiva/go-datastructures/common     [no test files]
ok      github.com/Workiva/go-datastructures/fibheap    1.669s
ok      github.com/Workiva/go-datastructures/futures    0.614s
ok      github.com/Workiva/go-datastructures/hashmap/fastinteger        1.228s
ok      github.com/Workiva/go-datastructures/list       2.105s
?       github.com/Workiva/go-datastructures/mock       [no test files]
ok      github.com/Workiva/go-datastructures/numerics/hilbert   0.640s
ok      github.com/Workiva/go-datastructures/numerics/optimization      1.128s
ok      github.com/Workiva/go-datastructures/queue      0.696s
ok      github.com/Workiva/go-datastructures/rangetree  1.715s
ok      github.com/Workiva/go-datastructures/rangetree/skiplist 1.274s
?       github.com/Workiva/go-datastructures/rtree      [no test files]
ok      github.com/Workiva/go-datastructures/rtree/hilbert      1.655s
ok      github.com/Workiva/go-datastructures/set        0.662s
ok      github.com/Workiva/go-datastructures/slice      0.761s
ok      github.com/Workiva/go-datastructures/slice/skip 1.057s
ok      github.com/Workiva/go-datastructures/sort       1.279s
ok      github.com/Workiva/go-datastructures/threadsafe/err     0.714s
ok      github.com/Workiva/go-datastructures/tree/avl   1.533s
ok      github.com/Workiva/go-datastructures/trie/ctrie 13.429s
ok      github.com/Workiva/go-datastructures/trie/dtrie 0.713s
ok      github.com/Workiva/go-datastructures/trie/xfast 1.482s
ok      github.com/Workiva/go-datastructures/trie/yfast 1.070s

C:\Users\Administrator\go\src\github.com\Workiva\go-datastructures>

5. 简单使用

打开 ”LiteIDE“ ,  文件 -> 打开目录 -> 选择 "C:\Users\Administrator\go\src" 下的 github.com 目录

效果如下:

右击 LiteIDE 中的  "github.com" 选择  “新建目录” 输入 "src" -> 点击“新建”

右击 “src” 目录 选择 “新建文件”输入 “main.go” 点击 “新建并编辑”

测试代码如下:

package main

import (
	"fmt"

	"github.com/Workiva/go-datastructures/list"
)

func main() {
	ll := list.Empty
	fmt.Println(ll.IsEmpty())

	ll = ll.Add(10)
	ll = ll.Add(12)
	ll = ll.Add(14)
	fmt.Println(ll.Length())

	var i uint
	for i = 0; i < ll.Length(); i++ {
		item, ok := ll.Get(i)
		if ok {
			fmt.Println("item(", i, ") = ", item)
		}
	}
	fmt.Println("------------------------------------")

	tmp, err := ll.Insert("Hello World!", ll.Length())
	if err != nil {
		fmt.Println("list insert err: ", err)
	} else {
		ll = tmp
	}

	for i = 0; i < ll.Length(); i++ {
		item, ok := ll.Get(i)
		if ok {
			fmt.Println("item(", i, ") = ", item)
		}
	}
	fmt.Println("------------------------------------")
}

右击 “src” 选择 “锁定编译目录” 编译运行,输出如下:

true
3
item( 0 ) =  14
item( 1 ) =  12
item( 2 ) =  10
------------------------------------
item( 0 ) =  14
item( 1 ) =  12
item( 2 ) =  10
item( 3 ) =  Hello World!
------------------------------------
成功: 进程退出代码 0.

 


有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:痞子汤

查看原文:GO 使用开源的数据结构

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1462 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传