What s the syntax logic initializing single var vs array

so we have

i := int(10)
j := [] int{1,2,3}

Just wondering why the use of curlies to initialize the array. Or vise versa, why not use curlies to initialize single variable. What is the syntax logic?

E.g. why couldn’t they use with curlies. This way its consistent.

i := int{10}

If have the syntax you said, I can finally get a number address.
I used to like this before:

i := 2
a := &i
// a := &2 is wrong.

now I can do this:

a := &int{1}

Yep you are right. We speak differently :). I am asking if anyone knows why the designers of Go decided to use that syntax.

You can simply do this:

i := 10

to initialise an int variable. int(10) is a typecast, and it’s unneeded as the compiler assumes 10 is an int by default. Typecasts are like functions in go.

1 Like

You can, in fact, use curlies to initialize a single value (not variable). e.g.

i := []int{10}

That will give you a single value in a slice. The logic is that curly brackets in a value indicate that the value is an array or slice.

It could have been done using other symbols. Since you need to enclose multiple values, it made sense for it to be some pair of symbols that match up. I guess they could have used « » or a digraph like %[ ]%, but { } was familiar from its use for the same purpose in C.

3 Likes

My understanding is that the curly brace following a type is a way to indicate the compiler that you are passing a composite literal. This allow to assign value of different composite type at declaration time.

The same composite literal approach can be used to assign any composite type at declaration time ( array, struct, pointer, function, interface, slice, map, and channel types).

I believe the designer of golang selected that way so it make it very easy to assign anything at declaration time in a consistent way. Actually the only thing that cannot be assigned as a composite literal are value of a predeclared type built in the language itseff (int, string, bool, float, etc ). Composite literal can also be nested allowing to compose complex assignment.

By the way, I am very much new to all that and not authoritative in anyway. Hope it make sense, if not try reading the code explaining it to yourself using the word used on effective go site.

Maybe an example can help me explain better how it help consistency:

package main
import (
	"fmt"
)

func main() {

	// predeclared type are assigned directly
	i := 10
	b := true
	s := "some string"
	
	// Anything that use a predeclared type to "compose" a type need to use
	// a composite literal to assign at declaration time.
	
	// e.g. a value of a type slice of int.  Mean a composite formed with the underlying 
	// type "int" in a slice
	xi := []int{42, 43, 44}
	
	// composite literal to assign a value of type []string
	xs := []string{"some", "string", "in", "a", "slice"}

	// composite literal to assign a value to type map[string]string
	m := map[string]string{"some key": "some value"}

	// composite literal to assign a value to type map[string][]string
	mss := map[string][]string{"some key": []string{"some", "string", "in", "a", "slice"}}

	// composite literal to assign an anonymous struct
	s2 := struct {
		first string
		last  string
	}{
		first: "James",
		last:  "bond"}

	// composite literal to assign an anonymous func
	f := func() {
		fmt.Println("an anonynous func()")
	}

	// using the value
	fmt.Println(i,b,xi, xs, s, m, mss, s2)
	f()
}
3 Likes

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