Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have the following Go snippet. I have a slice
package main
import "fmt"
func main() {
y := []int{0, 1, 1, 2, 3, 5, 8}
return y
}
func toParseY(int y) {
for i, v := range y {
fmt.Println(i, v)
}
}
I know that I have to pass the slice as argument to the toParseY function, but when I try it I get this error (the compiler doesn't even get the
# command-line-arguments
./ude.go:8:2: too many arguments to return
have ([]int)
want ()
./ude.go:11:19: undefined: y
./ude.go:12:20: undefined: y
What's the proper way to achieve it in Go?
Here is the corrected code...
package main
import "fmt"
func main() {
y := []int{0, 1, 1, 2, 3, 5, 8}
toParseY(y)
}
func toParseY(y []int) {
for i, v := range y {
fmt.Println(i, v)
}
}
Issues:
Don't return y from main. Instead, call toParseY() from main.
You needed to fix the data type that toParseY accepts to be a slice of int and correct the order in which you declare the parameter. It should be parameter name, followed by data type: toParseY(y []int)
Output:
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I recently started learning to Go, and I am quite confused as to why it has a strange "non-traditional" syntax unlike other languages (C, C++, C#, JAVA)
For example, a code like this in Go:
package main
import "fmt"
func sum(a int, b int) int {
return a + b
}
func main() {
numbers := [4] int {1, 2, 3, 4}
for _,n := range numbers {
result := sum(n, 2)
fmt.Println(result)
}
}
But, could be written something like in some languages:
package main
import "io"
int sum(int a, int b) {
return a + b
}
void main() {
int numbers[4] = {1, 2, 3, 4}
foreach (n in range(numbers)) {
result = sum(n, 2)
io.print(result)
}
}
So my question is, is there any technical reason behind this syntax, or is it just a personal preference of the team? Especially that the team behind Go used "C Language" to write Go, which means it would've made much more sense to type it in C-Style syntax ?
Few points that I'd like to highlight:
Go is inspired by many languages and not just C.
C: statement and expression syntax
Pascal: declaration syntax
Modula 2, Oberon 2: packages
CSP, Occam, Newsqueak, Limbo, Alef: concurrency
BCPL: the semicolon rule
Smalltalk: methods
Newsqueak: <-, :=
APL: iota
There are more
From when foreach and range become C-style syntax?
Third, don't confuse "For" statements with for clause and range clause. Read the spec.
In Go, you can do this is as well:
for i := 0; i < len(numbers); i++
But range clause is much more powerful once you understand it and yes it is not strange syntax. I'd suggest to read the spec and see a few examples.
Also, it's Go and not GoLang (Read). Always prefer the former over the latter.
Try the Go Tour. Some concepts are explained well.
Also, read Go's FAQ and Pike's blog on declaration syntax. The FAQ should answer many such queries.
import "io"
Go has fmt and io packages, although they do have some overlap. For example, fmt.Fprint lets you write to any io.Writer, and fmt.Fscan lets you read from any io.Reader.
Similarly you can write to console with io.Copy(os.Stdout, something), and read from console with io.Copy(something, os.Stdin).
int sum(int a, int b) {
I think I read that by having func first, it makes lexical parsing much faster. Also Go function can have named return values:
func sum(a int, b int) (n int)
I am not sure how you'd do that with the other syntax.
int numbers[4] = {1, 2, 3, 4}
Go syntax allows you to omit the type, which you can't do with C.
foreach (n in range(numbers))
Go doesn't have a while keyword, for the reason that less keywords again makes for faster lexical parsing. Instead you have different for invocations:
var n int
for {
if n > 9 {
break
}
println(n)
n++
}
var n int
for n < 9 {
println(n)
n++
}
for n := 0; n < 9; n++ {
println(n)
}
for range numbers {
println("hello")
}
for index := range numbers {
println(index)
}
for index, value := range numbers {
println(index, value)
}
For this:
result = sum(n, 2)
Go has two different syntax for variable assignment:
result := 1
result = 2
First is a declaration, second is assigning to an already declared variable.
io.print(result)
fmt.Println is uppercase, because any function that starts with an uppercase letter is a "public" function. This saves on typing public or pub everywhere.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am new to golang and while running this code snippet I am getting the len as 4, trying to understand why so ?
package main
import "fmt"
type phone struct {
model string
camera Camera
ram int
}
type Camera struct {
lens string
aparature int
}
func main() {
var m = make(map[string]phone)
myphn1 := phone{model: "iphone", camera: Camera{"20", 4}, ram: 6}
myphn2 := phone{model: "pixel", camera: Camera{"50", 2}, ram: 6}
m["myphn1"] = myphn1
m["myphn2"] = myphn2
var k = make([]string, len(m))
for key, _ := range m {
k = append(k, key)
}
fmt.Println(k)
fmt.Println(len(k))
}
I understand this adds size of 2 while creating, but while printing it gives somelike this , is the space in answer for 2 unallocated entries ?
[ myphn2 myphn1]
4
This creates a slice of length 2 (len(m) is 2 here):
var k = make([]string, len(m))
This adds two elements to it, for a total of 4:
for key, _ := range m {
k = append(k, key)
}
If you want to preallocate a slice, you need to provide a length of zero along with the desired capacity:
var k = make([]string, 0, len(m))
This is covered with examples in the Tour of Go.
You create a slice with length 2, and appended two more elements to it, so the length is 4.
what you probably want to do is to create a slice with capacity 2:
var k = make([]string,0,len(m))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I can't figure out why this Go code with pointers and using the switch statement prints out "a":
import "fmt"
func main() {
var a, b int
var c = &b
switch *c {
case a:
fmt.Println("a")
case b:
fmt.Println("b")
default:
fmt.Println("c")
}
}
Any hints to this?
a and b are both zero (the zero value for integers).
Initializing c = &b means that c is a *int pointing to b.
Moving on to the switch statement, we are checking the value of *c which dereferences c and is the value of b, which is 0.
Since a is also zero, the first case matches.
You can swap the case a and case b statements and put b first. In that case, it will print b since it is now the first matching case:
package main
import "fmt"
func main() {
var a, b int
var c = &b
switch *c {
case b:
fmt.Println("b")
case a:
fmt.Println("a")
default:
fmt.Println("c")
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
package main
import "fmt"
func main() {
var s []int
s = append(s, 2, 3, 4)
printSlice(s)
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
Playground: https://play.golang.org/p/tx8gmx5eR7B
Output:
len=3 cap=4 [2 3 4]
Output should be:
2
3
4
I want it without loop.
print elements in slice without bracket in Go line by line.
Comment: I want it without loop
For example,
package main
import "fmt"
func printSlice(s []int) {
if len(s) == 0 {
return
}
fmt.Println(s[0])
printSlice(s[1:])
}
func main() {
var s []int
s = append(s, 2, 3, 4)
printSlice(s)
}
Playground: https://play.golang.org/p/ISXdbjTrfSt
Output:
2
3
4
Create the string with the values and brackets.
Print the substring not including the brackets, and voilà.
Or use strings.Trim as suggested by JimB in the comment.
temp := fmt.Sprintf("%v", s)
fmt.Printf("len=%d cap=%d \n%v\n", len(s), cap(s), strings.Join(strings.Split(temp[1:len(temp)-1], " "), "\n"))
Outputs:
len=3 cap=4
2
3
4
Something like this will work if you don't want to use fmt.Println and only use printf and range over the slice without calculating len.
package main
import "fmt"
func main() {
var s []int
s = append(s, 2, 3, 4)
printSlice(s)
}
func printSlice(s []int) {
for _, value:= range s {
fmt.Printf("%v\n", value)
}
}
update: I haven't read your comments section while posting this , without for loop answer from peterSO works great and nice answer too.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
package main
import (
"fmt"
"math"
"reflect"
)
type Vertex struct {
X, Y float64
}
func (v *Vertex) Scale(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func main() {
v := &Vertex{3, 4} // Whether or not with "&", the values don't change below.
fmt.Printf("Before scaling: %+v, Abs: %v\n", v, v.Abs())
v.Scale(5)
fmt.Printf("After scaling: %+v, Abs: %v\n", v, v.Abs())
fmt.Println(reflect.TypeOf(Vertex{3,4}))
}
Hello, I am learning golang now. I do not understand what is the use of adding "&", if it does not make any change on the result value?
I thought we add "&" to variables to get the memory address. If we can add "&" to Vertex{3,4}, does this mean it is variable? Confused.
I assume you're talking about Vertex vs &Vertex? Yes, adding & means that v now contains an address to a struct of type Vertex, whereas without the &, v would hold the struct directly.
In your example, using the address, or the struct directly, makes no difference. In many other cases, the distinction is very important.