slice

Go的reflect机制和reflect包

时光怂恿深爱的人放手 提交于 2019-12-05 00:52:31
Go 的 reflect 机制和 reflect 包 概述 虽然 Go 是静态语言,然而还是提供了 reflect 机制,并且定义了 reflect 包来辅助反射处理。在 reflect 包中,最重要的两个类型就是 Type和Value,分别从类型、值的角度来描述一个 Go 对象。 Type 类型是一个接口,这个接口实现了 String() string 方法。 Value 类型是一个结构,但是并未定义任何导出字段,同样定义了 String() string 方法。 使用如下语句来导入 reflect 包 import "reflect" 注意 : 1.本文中出现在类型章节的函数,都是 Type 的方法。由于接口的特殊性,无法明确其 receiver 是指针还是值,所以并未显示其 receiver ,但都是有 receiver 的 2.很多方法都有要求,如果要求不满足,那么 panic 3.Value 类型也有零值,零值只能调用 IsValid()/String()/Kind() 方法,其余方法都会 panic 下面我们将依次介绍不同类型的对象和它对应的 Type/Value 对象 Go 的 reflect 机制和 reflect 包 1 类型 3 值 4 算术类型的 Go 对象 : 6 Go 的 reflect 机制和 reflect 包 1 类型 3 Kind 类型 4 值 4

Golang: get the type of slice

天涯浪子 提交于 2019-12-05 00:51:10
I am using reflect package to get the type of arbitrary array, but getting prog.go:17: cannot use sample_array1 (type []int) as type []interface {} in function argument [process exited with non-zero status] How do I get the type from array? I know how to get it from value. func GetTypeArray(arr []interface{}) reflect.Type { return reflect.TypeOf(arr[0]) } http://play.golang.org/p/sNw8aL0a5f Change: GetTypeArray(arr []interface{}) to: GetTypeArray(arr interface{}) By the way, []int is not an array but a slice of integers. The fact that you're indexing the slice is unsafe - if it's empty, you'll

How to calculate space between dicom slices for MPR?

给你一囗甜甜゛ 提交于 2019-12-05 00:45:25
问题 Due to showing MPR view based on Dicoms. I've made a 3D array from series of dicom files. And I show it from Coronal and Sagittal sides. My 3D array includes: - z = count of dicoms - c = column value for every dicoms - r = Row value for every dicoms But I have a problem. When there is some space between slices, image is made by this way doesn't show a correct view. Because I can not think of simulation distance between them! I don't know how to calculate space between slices? I want to add

golang中一些基础用法

佐手、 提交于 2019-12-05 00:25:10
range类似迭代器操作,返回 (索引, 值) 或 (键, 值)。其可以使用的对象包括string,array/slice,map,channel。其中string,array/slice返回的第一个value都是index,第二个value表示值;map返回第一个元素为key,第二个为value;channel中第一个值为元素的值。 举例如下: func main() { str := "abcdef" for key, value := range str { fmt.Printf("%d,%c\n", key, value) } array := [3]int{0, 1, 2}//注意,针对array,range 会复制对象。 for i, v := range array { if i == 0 { // index、value 都是从复制品中取出。 // 在修改前,我们先修改原数组。 array[1], array[2] = 999, 999 fmt.Println(array) // 确认修改有效,输出 [0, 999, 999]。 } array[i] = v + 100 // 使用用复制品中取出的 value 修改原数组。 } fmt.Println(array) // 输出 [100, 101, 102]。 //slice 为引用

golang多个routine操作map或者slice需要上锁

妖精的绣舞 提交于 2019-12-04 23:58:16
最近写服务器代码,运行期间突然panic,最后找到原因是因为map不是线程安全的,那这就简单啦,来一把读写锁。以下附上代码: type QueryExchangerFlowBySomeTime struct { com.HandlerDemo } func (self *QueryExchangerFlowBySomeTime) FuckBody (body interface {}) (result interface {}, err error ) { obj := *body.(* map [ string ] interface {}) ip :=to. String (obj[ "ip" ]) granule :=to. Int64 (obj[ "granule" ]) log. Println ( "传入:" ,granule) retMap := make ( map [ string ] interface {}) retMap[ "starttime" ]= GetHisTime () ips := GetAllMac (ip) retData :=ReqDataHub{} time_out := make ( chan int , 2 ) go ReqTimeOut (time_out, 1 ,DATAHUB_REQ_TIME) var wg sync

Golang基础学习(与java对比)

吃可爱长大的小学妹 提交于 2019-12-04 23:19:05
Golang基础 slice ​ 创建一个slice(切片) slice := [] int {lenth, 'b' , 'c' , 'd' } 与java数组不同的是: arr := [5 ] int {1 ,2 ,3 } 必须定死长度,否则会报错 non-constant array bound length slice 有一种是java中数据和list的结合体的感觉 对于 slice 有几个有用的内置函数: len 获取 slice 的长度 cap 获取 slice 的最大容量 append 向 slice 里面追加一个或者多个元素,然后返回一个和 slice 一样类型的 slice copy 函数 copy 从源 slice 的 src 中复制元素到目标 dst ,并且返回复制的元素的个数 ​ 总结:slice 和数据的区别在于 有没有定义长度 map ​ 创建一个map(与java基本类似),只是通过 maps1[key1] 获得结果的时候,有第二个参数(布尔值),可以判断是否这个key是不是存在,当然也可以只返回一个参数 maps1 := make ( map [ string ] string ) key1 := "key1" maps1[key1] = key1 s,ok := maps1[key1] if ok { fmt.Println( "hava key [

substrings and the Go garbage collector

陌路散爱 提交于 2019-12-04 22:28:50
问题 When taking a substring of a string in Go, no new memory is allocated. Instead, the underlying representation of the substring contains a Data pointer that is an offset of the original string's Data pointer. This means that if I have a large string and wish to keep track of a small substring, the garbage collector will be unable to free any of the large string until I release all references to the shorter substring. Slices have a similar problem, but you can get around it by making a copy of

Merge Maps in Golang

走远了吗. 提交于 2019-12-04 20:43:50
I need to merge multiple maps map1 = [ id: id_1 val: val_1 ] , map2 = [ id: id_2 val: val_2 ] and map3 = [id: id_1, val: val_3] such that the result map should be merged on the id values: result_map = [id: id_1 val: {val_1, val_3}, id: id_2 var: {val_2}} ] The code I've tried: var a = make(map[string]interface{}) for _, m := range data { for _, n := range data { if m["id"] == n["id"] { for l, k := range n { c[l] = k } } } } Is there a way this can be done? Am using Golang 1.7 Thanks Simple merge Yes, they can be merged, but since in the result map there may be multiple values associated to the

How to slice a n dimensional array with a m*(n-i) dimensional matrix?

强颜欢笑 提交于 2019-12-04 20:26:43
If i have a n dimensional array it can be sliced by a m * n matrix like this a <- array(1:27,c(3,3,3)) b <- matrix(rep(1:3,3),3) # This will return the index a[1,1,1] a[2,2,2] and a[3,3,3] a[b] # Output [1] 1 14 27 Is there any "effective and easy" way to do a similar slice but to keep some dimensions free? That is slice a n dimensional array with a m * (n-i) dimensional array and get a i+1 dimensional array as result. a <- array(1:27,c(3,3,3)) b <- matrix(rep(1:2,2),2) # This will return a vector of the index a[1] a[2] a[1] and a[2] a[b] # Output [1] 1 2 1 2 # This will return the indexes of

What is considered “small” object in Go regarding stack allocation?

左心房为你撑大大i 提交于 2019-12-04 19:31:19
The code: func MaxSmallSize() { a := make([]int64, 8191) b := make([]int64, 8192) _ = a _ = b } Then run go build -gcflags='-m' . 2>&1 to check memory allocation details. The result: ./mem.go:10: can inline MaxSmallSize ./mem.go:12: make([]int64, 8192) escapes to heap ./mem.go:11: MaxSmallSize make([]int64, 8191) does not escape My question is why a is small object and b is large object? make 64KB will escape to heap and less will allocate in stack. Does the _MaxSmallSize = 32 << 10 is the reason? go env GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="