map中的結構體元素是無法取位址的,即:map[string]struct型別,取&map["tmp"]是錯誤的。會提示報錯:
cannot assign to struct field elem["count"].count in map。
原因為:map中的元素並不是乙個變數,而是乙個值。因此,我們不能對map的元素進行取址操作。
package main
import (
"fmt"
"time"
)// counter 代表計數器的型別。
type counter struct
var mapchan = make(chan map[string]counter, 1)
func main() , 2)
go func() else
} fmt.println("[receiver] stopped.")
syncchan <- struct{}{} // 傳送同步通知
}()go func() ,
} for i := 0; i < 5; i++
close(mapchan)
syncchan <- struct{}{} // 傳送同步通知
}()<-syncchan //接收同步通知
<-syncchan //接收同步通知
}
執行結果如下所示:
[receiver] the elem is: map[count:]. elem address: 0xc000078000
[receiver] the counter: 1.
[sender] the count map: map[count:]. count map address: 0xc00000c030
[sender] the counter: 0.
[receiver] the elem is: map[count:]. elem address: 0xc000078010
[receiver] the counter: 1.
[sender] the count map: map[count:]. count map address: 0xc00000c030
[sender] the counter: 0.
[receiver] the elem is: map[count:]. elem address: 0xc0000a0000
[receiver] the counter: 1.
[sender] the count map: map[count:]. count map address: 0xc00000c030
[sender] the counter: 0.
[receiver] the elem is: map[count:]. elem address: 0xc00000c040
[receiver] the counter: 1.
[sender] the count map: map[count:]. count map address: 0xc00000c030
[sender] the counter: 0.
[receiver] the elem is: map[count:]. elem address: 0xc000078020
[receiver] the counter: 1.
[sender] the count map: map[count:]. count map address: 0xc00000c030
[sender] the counter: 0.
[receiver] stopped.
package main
import (
"fmt"
"time"
)// counter 代表計數器的型別。
type counter struct
var mapchan = make(chan map[string]*counter, 1)
func main() , 2)
go func() else
} fmt.println("[receiver] stopped.")
syncchan <- struct{}{}
}()go func() ,
} for i := 0; i < 5; i++
close(mapchan)
syncchan <- struct{}{}
}()<-syncchan
<-syncchan
}
執行結果如下所示:
[receiver] the elem is: map[count:0xc000074008]. elem address: 0xc00008a000
[receiver] the counter: 1.
[sender] the count map: map[count:0xc000074008]. count map address: 0xc000082020
[sender] the counter: 1.
[receiver] the elem is: map[count:0xc000074008]. elem address: 0xc00008a010
[receiver] the counter: 2.
[sender] the count map: map[count:0xc000074008]. count map address: 0xc000082020
[sender] the counter: 2.
[receiver] the elem is: map[count:0xc000074008]. elem address: 0xc00000c010
[receiver] the counter: 3.
[sender] the count map: map[count:0xc000074008]. count map address: 0xc000082020
[sender] the counter: 3.
[receiver] the elem is: map[count:0xc000074008]. elem address: 0xc00000c018
[receiver] the counter: 4.
[sender] the count map: map[count:0xc000074008]. count map address: 0xc000082020
[sender] the counter: 4.
[receiver] the elem is: map[count:0xc000074008]. elem address: 0xc00000c020
[receiver] the counter: 5.
[sender] the count map: map[count:0xc000074008]. count map address: 0xc000082020
[sender] the counter: 5.
[receiver] stopped.
package main
import (
"fmt"
"time"
)var mapchan = make(chan map[string]int, 1)
func main() , 2)
go func() else
} fmt.println("[receiver] stopped.")
syncchan <- struct{}{} // 傳送同步通知
}()go func()
close(mapchan) // 關閉通道
syncchan <- struct{}{} // 傳送同步通知
}()<-syncchan //接收同步通知
<-syncchan //接收同步通知
fmt.println(" map為引用型別,傳送和接收的元素指向同乙個值.")
}
執行結果如下所示:
[receiver] the elem is: map[count:2]. elem address is: 0xc000078000
[sender] the count map: map[count:2]. count map address: 0xc00000c030
[receiver] the elem is: map[count:4]. elem address is: 0xc000078010
[sender] the count map: map[count:4]. count map address: 0xc00000c030
[receiver] the elem is: map[count:6]. elem address is: 0xc00000c040
[sender] the count map: map[count:6]. count map address: 0xc00000c030
[receiver] the elem is: map[count:8]. elem address is: 0xc0000a4000
[sender] the count map: map[count:8]. count map address: 0xc00000c030
[receiver] the elem is: map[count:10]. elem address is: 0xc000078020
[sender] the count map: map[count:10]. count map address: 0xc00000c030
[receiver] stopped.
map為引用型別,傳送和接收的元素指向同乙個值.
STL中list結構體元素排序
stl中list結構體元素排序 include using namespace std 結構體定義 typedef structst list test 比較類 class sortlist less else if stleft.inum1 stright.inum1 else if stleft...
如何通過結構體元素找到結構體?
我們知道,如果有乙個結構體定義如下 struct st st 我們可以通過st訪問到a或者b,方法就是st.a 或者如果有st的指標pst,那麼就用pst a 但是,如果知道了結構體中元素的指標,是否可以獲得當前結構體的指標呢?或者說,如果我只能訪問到b,我可以訪問到st和a麼?首先,這樣做有什麼用...
結構體中巢狀結構體
結構體的巢狀問題 結構體的自引用 self reference 就是在結構體內部,包含指向自身型別結構體的指標。結構體的相互引用 mutual reference 就是說在多個結構體中,都包含指向其他結構體的指標。1.1不使用typedef時 錯誤的方式 struct tag 1 這種宣告是錯誤的,...