2012-10-17 36 views
8
結構

我遇到了一個錯誤,而執行下面的代碼:點在Golang

package main 

import (
    "fmt" 
) 

type Struct struct { 
    a int 
    b int 
} 

func Modifier(ptr *Struct, ptrInt *int) int { 
    *ptr.a++ 
    *ptr.b++ 
    *ptrInt++ 
    return *ptr.a + *ptr.b + *ptrInt 
} 

func main() { 
    structure := new(Struct) 
    i := 0   
    fmt.Println(Modifier(structure, &i)) 
} 

這給了我一個錯誤一些有關「無效間接ptr.a的(類型爲int)......」。另外爲什麼編譯器不給我關於ptrInt的錯誤?提前致謝。

回答

13

只是做

func Modifier(ptr *Struct, ptrInt *int) int { 
    ptr.a++ 
    ptr.b++ 
    *ptrInt++ 
    return ptr.a + ptr.b + *ptrInt 
} 

你實際上是試圖在*(ptr.a)ptr.a申請++是一個int,而不是一個指針爲int。如果ptr是一個指針,這就是爲什麼你沒有->在Go中。

+1

感謝您的回答,這對我很有幫助。 – Coder