M
M
Michael2017-01-30 12:45:24
go
Michael, 2017-01-30 12:45:24

Why can't I change a field inside a function?

Sorry for the stupid question. Just started to get familiar with Go. There is this code:

package main

import "fmt"
import "math"

type Circle struct {
    x int
    y int
    r float64
}

func circleArea(c *Circle) float64 {
    с.r = 10
    return math.Pi * c.r * c.r
}

func main() {
    c:= Circle{1, 2, 3}
    fmt.Println(circleArea(&c))
}

In the console I see:
./hello.go:13: undefined: with in c.r
./hello.go:13: invalid indirect of 10 (type untyped number)
./hello.go:13: cannot assign to c.r
Why can't I change the value of a field? I take a pointer as input, and in theory the external variable should also change

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-01-30
@mak_ufo

You have "s" Russian in the lineс.r = 10

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question