-
golang: 常用数据类型底层结构分析
所属栏目:[大数据] 日期:2020-12-16 热度:122
虽然golang是用C实现的,并且被称为下一代的C语言,但是golang跟C的差别还是很大的。它定义了一套很丰富的数据类型及数据结构,这些类型和结构或者是直接映射为C的数据类型,或者是用C struct来实现。了解golang的数据类型和数据结构的底层实现,将有助于我[详细]
-
golang使用时间作为种子生成随机数
所属栏目:[大数据] 日期:2020-12-16 热度:147
packagemainimport("fmt""time""math/rand")funcmain(){r:=rand.New(rand.NewSource(time.Now().UnixNano()))fori:=0;i10;i++{fmt.Println(r.Intn(100))}}//该代码片段来自于:http://www.sharejs.com/codes/go/8974[详细]
-
如何在golang中将数据结构作为参数传递
所属栏目:[大数据] 日期:2020-12-16 热度:110
XML到Json package mainimport ( "encoding/json" "encoding/xml" "fmt")type Persons struct { Person []struct { Name string Age int }}type Places struct { Place []struct { Name string Country string }}type Parks struct { Park struct { Name []s[详细]
-
go(golang)测试框架中的“示例”有什么意义?
所属栏目:[大数据] 日期:2020-12-16 热度:167
我最近在go测试框架中阅读了 testing and examples,并没有真正理解它们的用途.我看到文档说: The package also runs and verifies example code. Example functions may include a concluding line comment that begins with “Output:” and is compared w[详细]
-
如何在golang中的字符串上使用gzip?
所属栏目:[大数据] 日期:2020-12-16 热度:167
我想使用go从文件中读出一个块,将其视为一个字符串并gzip这个块.我知道如何从文件中读取并将其视为字符串,但是当涉及到“compress / gzip”时,我迷失了.我应该创建一个写入buf(字节片)的io.writer,使用gzip.Writer(io.writer)获取指向io.writer的编写器指针,[详细]
-
单元测试 – 如何在golang中测试io.writer?
所属栏目:[大数据] 日期:2020-12-16 热度:131
最近我希望为golang写一个单元测试.功能如下. func (s *containerStats) Display(w io.Writer) error { fmt.Fprintf(w,"%s %sn","hello","world") return nil} 那么如何测试“func Display”的结果是“hello world”? 您可以简单地传入您自己的io.Writer并[详细]
-
如果为空,Golang隐藏XML父标记
所属栏目:[大数据] 日期:2020-12-16 热度:54
经过一些追踪和错误,我想分享我正在处理的问题. 我正在填充结构并将其转换为XML(xml.Marshal) 如下所示,Foo示例按预期工作.但是Bar示例创建了一个空group1. 所以我的问题是:“如果没有设置子代,我如何防止生成Group1.” package mainimport ( "fmt" "encodi[详细]
-
为什么这个Golang代码不能在多个时间内进行选择.
所属栏目:[大数据] 日期:2020-12-16 热度:162
为什么这个Golang代码不能在多个时间内进行选择. 见下面的代码.永远不会发出“超时”消息.为什么? package mainimport ( "fmt" "time")func main() { count := 0 for { select { case -time.After(1 * time.Second): count++ fmt.Printf("tick %dn",count)[详细]
-
在golang中,原始值的typedef是否等效?
所属栏目:[大数据] 日期:2020-12-16 热度:91
鉴于此代码: type Philosopher intconst ( Epictetus Philosopher = iota Seneca)func Quote(who Philosopher) string { fmt.Println("t: ",reflect.TypeOf(who)) switch who { case Epictetus: return "First say to yourself what you would be; and do w[详细]
-
在golang中使用json marshaling测试深度相等性
所属栏目:[大数据] 日期:2020-12-16 热度:53
鉴于这两个测试用例: func TestEqualWhat(t *testing.T) { testMarshalUnmarshal(t,map[string]interface{}{"a":"b"}) testMarshalUnmarshal(t,map[string]interface{}{"a":5})} testMarshalUnmarshal帮助器只是对json进行编组并返回: func testMarshalUnm[详细]
-
数组 – 在Golang中将两个或多个[] map [string] interface {}类
所属栏目:[大数据] 日期:2020-12-16 热度:97
我正在使用Golang,出于某种原因,我需要合并来自不同数据库查询的结果,所有这些都返回一个[] map [string] interface {} 我正在考虑追加,但如果这是可能的话,它还不够清楚. 我正在看的最终数据类型是什么? 显然,一个带键作为字符串的接口映射数组应该能够简[详细]
-
数组 – 测试索引超出范围golang
所属栏目:[大数据] 日期:2020-12-16 热度:160
很抱歉这个noob问题,但我不确定我如何测试我正在访问的元素是否对数组有效,请考虑以下设计代码: func main() { strings := []string{"abc","def","ghi","jkl"} for i := 0; i5; i++ { if strings[i] { fmt.Println(strings[i]) } }} https://play.golang.or[详细]
-
oop – 接口组成[Golang]
所属栏目:[大数据] 日期:2020-12-16 热度:71
有没有办法让接口还包含Go中另一个接口定义的方法? 例如: type BasicDatabase interface { CreateTable(string) error DeleteTable(string) error}type SpecificDatabase interface { CreateUserRecord(User) error} 我想要一种方法来指定SpecificDatabase[详细]
-
golang,Windows版本中当前正在运行的进程列表
所属栏目:[大数据] 日期:2020-12-16 热度:141
如何在 Windows下的golang中获取当前正在运行的进程列表? 我需要这样的东西: List of currently running process in Golang 但也可以在Windows下使用. 我刚刚实现了你需要的功能(EnumProcess as axw如上所述). 查看 https://github.com/AllenDang/w32.您可[详细]
-
在Golang中使用struct成员是常见的吗?
所属栏目:[大数据] 日期:2020-12-16 热度:169
以 go github package为例.几乎每个定义的结构的每个成员都是一个指向值而不是值的指针. 这是惯用的Go吗?为什么? 我知道它减少了结构的大小(假设指针的大小小于它指向的值的大小),如果你通过值大量传递结构,这可能很重要.但是为什么不使用值结构并通过指针[详细]
-
golang http响应标头被删除
所属栏目:[大数据] 日期:2020-12-16 热度:134
我不确定这是一个错误还是http响应包应该如何工作. 在此示例中,不会设置Content-Type响应标头 // Return the responsew.WriteHeader(http.StatusCreated)w.Header().Set("Content-Type","application/json")w.Write(js) 但是,如果我按顺序设置标题的顺序,它[详细]
-
Golang Fprint * f代表什么
所属栏目:[大数据] 日期:2020-12-16 热度:155
fmt包具有一些字符串格式化功能. Sprint *我很确定代表字符串打印. 打印*写入STDOUT F代表Fprint *的功能是什么? “f”代表“文件” – 它是一个C / Unix-ism.当然Fprint可以写入任意的Writer,但在Unix世界文件中是一个非常通用的概念,可以引用从“普通”文[详细]
-
比较Golang中的日期部分ot time.Time
所属栏目:[大数据] 日期:2020-12-16 热度:159
假设以下两个日期.日期是一样的,但时间不同. t1,_ := time.Parse("2006-01-02 15:04:05","2016-01-01 12:12:12.0")t2,"2016-01-01 18:19:20.0") 我会使用Format()来比较它们,但我不确定这是否是最好的方式,特别是当不同的时区在起作用时. if t1.Format("2006[详细]
-
GOLANG“Namespaced”枚举?
所属栏目:[大数据] 日期:2020-12-16 热度:121
我理解在GO中创建枚举的惯用方法如下: type topicStatus intconst ( registered topicStatus = iota active inactive pending-removal removed ) 但如果我有另一个想要“重用”名称的“枚举”,我会收到一个错误: type hotelVisit intconst ( registered ho[详细]
-
使用Golang mgo连接到MongoDB Atlas:持久无法访问服务器到副本
所属栏目:[大数据] 日期:2020-12-16 热度:93
我有一个来自 mongodb atlas的副本集,我可以使用任何其他语言与常??规的mongo客户端连接,并提供以下格式的url: “mongodb的://用户:pass@prefix1.mongodb.net:27017,prefix2.mongodb.net:27017,prefix3.mongodb.net:27017 /试验安培; replicaSet = Clu[详细]
-
Golang可以安全地切换cmd.Stdout
所属栏目:[大数据] 日期:2020-12-16 热度:179
我用Go执行进程并将输出写入文件(日志文件) cmd := exec.Command(path) cmd.Dir = dir t := time.Now() t1 := t.Format("20060102-150405") fs,err := os.Create(dir + "/var/log/" + t1 + ".std") if err == nil { cmd.Stdout = fs } 我希望每天轮换日志并[详细]
-
golang:运行时错误:无效的内存地址或无指针取消引用
所属栏目:[大数据] 日期:2020-12-16 热度:143
我是Golang的新手,目前正在遵循本教程和源代码 – http://golang.org/doc/articles/wiki/part2.go 构建此文件后,我得到了 calvin$./mywebwiki2 2012/07/23 17:12:59 http: panic serving [::1]:58820: runtime error: invalid memory address or nil pointer[详细]
-
如何处理用于CLI测试的“fmt”golang库包
所属栏目:[大数据] 日期:2020-12-16 热度:75
免责声明:祝你圣诞快乐,我希望我的问题不会打扰你! sample.go: package mainimport( "fmt" "os")type sample struct { value int64}func (s sample) useful() { if s.value == 0 { fmt.Println("Error: something is wrong!") os.Exit(1) } else { fmt.Pr[详细]
-
修改后如何重建本机Golang包?
所属栏目:[大数据] 日期:2020-12-16 热度:113
我修改了网络包,我想在我的应用程序中使用修改后的版本,但它继续使用旧代码. 这有效: $go install -a net 它也没有为我重建,但即使工具链认为指定的包是最新的,-a也会强制重建.[详细]
-
golang设定时间值.时间
所属栏目:[大数据] 日期:2020-12-16 热度:198
package main import ( "fmt" "reflect" ) func main() { type t struct { N int } var n = t{42} fmt.Println(n.N) reflect.ValueOf(n).Elem().FieldByName("N").SetInt(7) fmt.Println(n.N) } 下面的编程工作的问题是如何用time.Time类型像这样做 package[详细]
