我一直在試圖弄清楚為什么一個特定的行會編譯,而另一行不會。這是一個提煉的版本:type A *stringtype B *stringfunc TakeStringPointer(a *string) { fmt.Println("something: %s\n", *a)}func TakeA(a A) { fmt.Println("something else: %s\n", *a)}func Sample() { aa := "asdf" var pA A var pB B var pString *string pA = &aa pB = &aa pString = &aa TakeStringPointer(pString) TakeStringPointer(pA) TakeStringPointer(pB) TakeA(pA) TakeA(pB) // Does not compile TakeA(pString) // Does compile }據我了解,TakeA(pB)應該TakeA(pString)兩者都可以工作,或者兩者都不能工作......A value x is assignable to a variable of type T if:x’s type is identical to T.x’s type V and T have identical underlying types…在 go 規范中。對我來說,我希望兩者都能編譯,因為兩者都A具有B相同的基礎類型。(或者,兩者都不會,因為 *string 與 A 不同,因為我們有一個類型聲明)。這里發生了什么?
- 1 回答
- 0 關注
- 179 瀏覽
添加回答
舉報
0/150
提交
取消