Error: expected expression syntax

I’m getting this error: expected expression syntax Not sure why it’s happening. I’m new in Go programming. It’ll be wonderful if someone helps me out.

Here’s the code:

package main

import "fmt"

func main() {

    x := [5]int

    x[4] = 100

    fmt.Println(x)

}

I copied your code into and created a link by clicking the “Share” button.

When I run your code, I get an error:

./prog.go:7:10: type [5]int is not an expression

Go build failed.

That error is telling you that “type [5]int” is not an expression: It’s a type. It’s the same as saying x := string or x := int.

If your goal was to define a variable of type [5]int, then you have to write it as:

var x [5]int

You could rewrite this declaration into a “short form” like this:

x := [5]int{}

[5]int is a type, but [5]int{} is an expression which creates a [5]int array with all its elements set to the int default value: 0.

2 Likes

That’s really helpful! And really feeling awesome that this language has an outstanding community!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.