2017-08-17 46 views
-1

我在下面的例子中得到非數字類型* int錯誤,爲什麼?爲什麼我得到非數字類型* int錯誤?

func main() { 
    count := 0 
    for { 
     counting(&count) 
    } 
} 

func counting(count *int) { 
    fmt.Println(count) 
    count++ 
} 
+5

因爲'count'是'* int'。如果你刪除'count ++',你會看到你沒有打印你期望打印的內容。 – JimB

+0

那麼我該如何計算去func? – irom

+2

你不能增加一個指針,只能增加一個數字。您可以在增加值之前使用'* count ++'來使指針順從。 – Adrian

回答

4

你需要反引用指針:

package main 

import (
    "fmt" 
) 

func main() { 
    count := 0 
    for i:=0; i<10; i++ { 
     counting(&count) 
    } 
} 

func counting(count *int) { 
    fmt.Println(*count) 
    *count++ 
} 
+2

增量中不需要parens。 – Adrian

相關問題