go语言安装第三方程序包

来源:互联网 发布:ansible windows 编辑:程序博客网 时间:2024/06/05 01:14

这里拿我最近正在看的一个源码举例。

环境: win10+jetBrains Gogland+git bash客户端

安装第三方go-cache程序包

源码的地址

https://github.com/patrickmn/go-cache

Go 语言实现的一个内存中的缓存框架,实现 Key-Value 的序列存储,适用于单台机器应用程序。

安装前需要两个前提条件:

  1. 具备GOPATH环境变量,这个一般只要你安装好了go语言的IDE环境都应该有的。
  2. 安装一个git客户端(我的是windows环境,用的是git bash)

安装过程很简单,一条指令,

pony@DESKTOP-17T9AJU MINGW64 ~$ go get github.com/patrickmn/go-cache

安装完成没有任何的提示,为了确保成功我们可以去GOPATH环境变量对应的目录看下是否有安装文件。首先会在src目录下存在一个go-cache的源码目录,然后pkg目录下有个go-cache.a库文件。

测试

安装完成了,测试下。新建一个文件,

package mainimport (    "github.com/patrickmn/go-cache" // 从环境变量:%goPath% 中定义的路径去查找第三方类库    "fmt"    "time")func main()  {    // Create a cache with a default expiration time of 5 minutes, and which    // purges expired items every 10 minutes    c := cache.New(5*time.Minute, 10*time.Minute)    // Set the value of the key "foo" to "bar", with the default expiration time    c.Set("foo", "bar", cache.DefaultExpiration)    // Set the value of the key "baz" to 42, with no expiration time    // (the item won't be removed until it is re-set, or removed using    // c.Delete("baz")    c.Set("baz", 42, cache.NoExpiration)    // Get the string associated with the key "foo" from the cache    foo, found := c.Get("foo")    if found {        fmt.Println(foo)    }}

运行,

barProcess finished with exit code 0
原创粉丝点击