http://play.golang.org/p/jdWZ9boyrh我收到此錯誤 prog.go:29: invalid receiver type *[]Sentence ([]Sentence is an unnamed type) prog.go:30: cannot range over S (type *[]Sentence) [process exited with non-zero status]當我的函數(shù)嘗試接收結(jié)構(gòu)數(shù)組時。未命名類型是什么意思?為什么不能命名?我可以在函數(shù)之外命名它,也可以將它們作為參數(shù)傳遞給它們命名。這沒用。所以我只是通過了 []Sentence 作為參數(shù)并解決了我需要的問題。但是當將它們作為參數(shù)傳遞時,我不得不返回一個新副本。我仍然認為,如果我可以讓函數(shù)接收結(jié)構(gòu)數(shù)組并且不必返回任何內(nèi)容,那就太好了。像下面這樣:func (S *[]Sentence)MarkC() { for _, elem := range S { elem.mark = "C" }}var arrayC []Sentencefor i:=0; i<5; i++ { var new_st Sentence new_st.index = i arrayC = append(arrayC, new_st)}//MarkC(arrayC)//fmt.Println(arrayC)//Expecting [{0 C} {1 C} {2 C} {3 C} {4 C}] //but not working 它也不適用于 []Sentence。無論如何,我可以讓函數(shù)接收 Struct 數(shù)組嗎?
添加回答
舉報
0/150
提交
取消