如题,先上代码
package main
import (
"fmt"
"io/ioutil"
"strings"
)
var (
row1 = [][]string{
{"Q-843A磷化", "2016", "2015年结转", "0", "60"},
{"Q-843A磷化", "2016", "01月_结存", "0", "60"},
{"Q-843A磷化", "2016", "02月_结存", "0", "60"},
{"Q-843A磷化", "2016", "03月_结存", "0", "60"},
{"Q-843A磷化", "2016", "04月_结存", "0", "60"},
{"Q-843A磷化", "2016", "05月_结存", "0", "60"},
{"Q-843A磷化", "2016", "06月_结存", "0", "60"},
{"Q-843A磷化", "2016", "07月_结存", "0", "60"},
{"Q-843A磷化", "2016", "08月_结存", "0", "60"},
{"Q-843A磷化", "2016", "09月_结存", "0", "0"},
{"Q-843A磷化", "2016", "09月_生产", "0", "0"},
{"Q-843A磷化", "2016", "10月_结存", "0", "0"},
{"Q-843A磷化", "2016", "11月_结存", "0", "0"},
{"Q-843A磷化", "2016", "12月_结存", "0", "0"},
{"Q-843A磷化", "2017", "2016年结转", "0", "0"},
{"Q-843A磷化", "2017", "01月_结存", "0", "0"},
{"Q-843A磷化", "2017", "02月_结存", "0", "0"},
{"Q-843A磷化", "2017", "03月_", "-13", "0"},
{"Q-843A磷化", "2017", "03月_20161109-19", "109", "0"},
{"Q-843A磷化", "2017", "03月_20161109-19", "23", "0"},
{"Q-843A磷化", "2017", "03月_结存", "0", "119"},
{"Q-843A磷化", "2017", "04月_结存", "0", "119"},
{"Q-843A磷化", "2017", "05月_结存", "0", "119"},
{"Q-843A磷化", "2017", "06月_结存", "0", "119"},
{"Q-843A磷化", "2017", "07月_结存", "0", "119"},
{"Q-843A磷化", "2017", "08月_结存", "0", "119"},
{"Q-843A磷化", "2017", "09月_结存", "0", "119"},
{"Q-843A磷化", "2017", "10月_结存", "0", "119"},
{"Q-843A磷化", "2017", "11月_结存", "0", "119"},
{"Q-843A磷化 ", "2017", "12月", "27", "0"},
{"Q-843A磷化", "2017", "12月_结存", "27", "146"},
{"Q-843A磷化", "2018", "2017年结转", "0", "146"},
{"Q-843A磷化", "2018", "01月_结存", "0", "146"},
{"Q-843A磷化", "2018", "02月_结存", "0", "146"},
{"Q-843A磷化", "2018", "03月_结存", "0", "146"}}
)
type huopin struct {
name string
jiezhuan15 []string
jiezhuan16 []string
jiezhuan17 []string
isruku16 bool
isruku17 bool
isruku18 bool
jiecun16 [][]string
jiecun17 [][]string
jiecun18 [][]string
}
func MyJiangXu(src [][]string, n int) {
if len(src) == 1 {
return
}
for i := 0; i < len(src)-1; i++ {
for j := i + 1; j < len(src); j++ {
if src[i][n] < src[j][n] {
src[i], src[j] = src[j], src[i]
}
}
}
}
func main() {
var huopinMap = make(map[string]*huopin)
data, err := ioutil.ReadFile("F:\\workspace\\src\\mygo\\myApp1\\test1")
if err != nil {
fmt.Println("readfile err: ", err, data)
}
// for _, line := range strings.Split(string(data), "\n") {
// row := strings.Split(line, "\t")
for _, line := range row1 {
row := line
if len(row) < 2 {
fmt.Println(row)
continue
}
_, ok := huopinMap[row[0]]
if !ok {
huopinMap[row[0]] = new(huopin)
huopinMap[row[0]].name = row[0]
}
if strings.Contains(row[2], "2015年结转") {
huopinMap[row[0]].jiezhuan15 = row
}
if strings.Contains(row[2], "2016年结转") {
huopinMap[row[0]].jiezhuan16 = row
}
if strings.Contains(row[2], "2017年结转") {
huopinMap[row[0]].jiezhuan17 = row
}
if strings.Contains(row[2], "月_结存") {
if strings.Contains(row[1], "2016") {
huopinMap[row[0]].jiecun16 = append(huopinMap[row[0]].jiecun16, row)
}
if strings.Contains(row[1], "2017") {
huopinMap[row[0]].jiecun17 = append(huopinMap[row[0]].jiecun17, row)
}
if strings.Contains(row[1], "2018") {
huopinMap[row[0]].jiecun18 = append(huopinMap[row[0]].jiecun18, row)
}
}
if strings.Compare(row[3], "0") != 0 {
if strings.Contains(row[1], "2016") {
huopinMap[row[0]].isruku16 = true
}
if strings.Contains(row[1], "2017") {
huopinMap[row[0]].isruku17 = true
}
if strings.Contains(row[1], "2018") {
huopinMap[row[0]].isruku18 = true
}
}
}
fmt.Println(len(huopinMap))
for k, v := range huopinMap {
fmt.Println(k)
fmt.Println(&v)
if len(v.jiecun16) < 1 && len(v.jiecun17) < 1 && len(v.jiecun18) < 1 {
fmt.Println("len 16", len(v.jiecun16))
fmt.Println("len 17", len(v.jiecun17))
fmt.Println("len 18", len(v.jiecun18))
}
if len(v.jiecun16) > 0 {
MyJiangXu(v.jiecun16, 2)
}
if len(v.jiecun17) > 0 {
MyJiangXu(v.jiecun17, 2)
}
if len(v.jiecun18) > 0 {
MyJiangXu(v.jiecun18, 2)
}
fmt.Println("排序后:", v)
}
}
如题,输出信息,如下
2
Q-843A磷化
0xc042004038
排序后: &{Q-843A磷化 [Q-843A磷化 2016 2015年结转 0 60] [Q-843A磷化 2017 2016年结转 0 0] [Q-843A磷化 2018 2017年结转 0 146] false true false [[Q-843A磷化 2016 12月_结存 0 0] [Q-843A磷化 2016 11月_结存 0 0] [Q-843A磷化 2016 10月_结存 0 0] [Q-843A磷化 2016 09月_结存 0 0] [Q-843A磷化 2016 08月_结存 0 60] [Q-843A磷化 2016 07月_结存 0 60] [Q-843A磷化 2016 06月_结存 0 60] [Q-843A磷化 2016 05月_结存 0 60] [Q-843A磷化 2016 04月_结存 0 60] [Q-843A磷化 2016 03月_结存 0 60] [Q-843A磷化 2016 02月_结存 0 60] [Q-843A磷化 2016 01月_结存 0 60]] [[Q-843A磷化 2017 12月_结存 27 146] [Q-843A磷化 2017 11月_结存 0 119] [Q-843A磷化 2017 10月_结存 0 119] [Q-843A磷化 2017 09月_结存 0 119] [Q-843A磷化 2017 08月_结存 0 119] [Q-843A磷化 2017 07月_结存 0 119] [Q-843A磷化 2017 06月_结存 0 119] [Q-843A磷化 2017 05月_结存 0 119] [Q-843A磷化 2017 04月_结存 0 119] [Q-843A磷化 2017 03月_结存 0 119] [Q-843A磷化 2017 02月_结存 0 0] [Q-843A磷化 2017 01月_结存 0 0]] [[Q-843A磷化 2018 03月_结存 0 146] [Q-843A磷化 2018 02月_结存 0 146] [Q-843A磷化 2018 01月_结存 0 146]]}
Q-843A磷化
0xc042004038
len 16 0
len 17 0
len 18 0
排序后: &{Q-843A磷化 [] [] [] false true false [] [] []}
为什么这个huopinMap[Q-843A磷化] 这个会有2个?并且地址还相同,里面内容 string bool 类型的值也相同,但是切片 类型的一个有值,一个确实空。
按说这个huopinMap[Q-843A磷化] 不是应该就一个吗?为啥会出现2的????
求解,谢谢
<a href="/user/marlonche" title="@marlonche">@marlonche</a> 谢谢了,意思是 v的局部变量地址 跟 map里的 每个值的地址 不一样的?
#6
更多评论