non-name c[0] on left side of :=

2,682 views
Skip to first unread message

Sugatang Itlog

unread,
Jul 6, 2014, 10:57:30 PM7/6/14
to golan...@googlegroups.com
Hi new in Go and am using the ebook (and excercises) from http://www.miek.nl/downloads/Go/Learning-Go-latest.pdf. And am still at chapter 1.

Though am getting the error ... when running go run adrihello.go.

[root@localhost GoTutorial]# go run adrihello.go
# command-line-arguments
./adrihello.go:8: non-name c[0] on left side of :=
[root@localhost GoTutorial]#

And here is the code

[root@localhost GoTutorial]# cat adrihello.go
package main

import "fmt"

func main() {
  s := "hello";
  c := []rune(s);
  c[0] := 'c';
  s2 := string(c);
  fmt.Printf("%s\n", s2);
}
[root@localhost GoTutorial]#

Thanks and really appreciate for the time and assistance.

Regards,
SGTItlog

Sugatang Itlog

unread,
Jul 6, 2014, 10:59:28 PM7/6/14
to golan...@googlegroups.com
Here is the version that am using ...

[root@localhost GoTutorial]# go version
go version go1.2.1 linux/amd64
[root@localhost GoTutorial]#

Thanks and Regards,
SGTItlog

Matt Silverlock

unread,
Jul 6, 2014, 11:10:08 PM7/6/14
to golan...@googlegroups.com
:= is short-hand for "var name = thing", and both declares and assigns the "thing" to a variable of "name" (the type is inferred). You can't declare a slice index (what c[0] is) or a map key.

See http://play.golang.org/p/8lKqawXeHK - just change := to a simple = assignment operator.

mikespook

unread,
Jul 6, 2014, 11:15:26 PM7/6/14
to Sugatang Itlog, golang-nuts
I think you are using an old version of "Leaning GO". 

Here's the source code form the latest version, page 6:

s := "hello"
c := []rune(s)
c[0] = 'c'

Two differences:
1. No semicolon at the end of lines.
2. It is `c[0] = 'c'` but `c[0] := 'c'`


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Xing Xing (邢星)
mikespook <mike...@gmail.com>
http://mikespook.com

Sugatang Itlog

unread,
Jul 6, 2014, 11:17:43 PM7/6/14
to golan...@googlegroups.com
Thank you and noted on that. To much typing on my side ^_^.And nice link and will rather use it for the meantime.

Thanks and Regards,
SGTItlog
Reply all
Reply to author
Forward
0 new messages