68599

GO explicit array initialization

Question:

Is there explicit array initialization (declaration and assignment) in GO or the only way is using the shorthand operator? Here is a practical example - is this two equal:

a := [3]int{1, 0, 1} var a [3]int = [3]int{1, 0, 1}

Answer1:

They are equivalent. In general: <a href="https://golang.org/ref/spec#Short_variable_declarations" rel="nofollow">Spec: Short variable declaration:</a>

<blockquote>

A <em>short variable declaration</em> uses the syntax:

ShortVarDecl = IdentifierList ":=" ExpressionList .

It is shorthand for a regular <a href="https://golang.org/ref/spec#Variable_declarations" rel="nofollow">variable declaration</a> with initializer expressions but no types:

"var" IdentifierList = ExpressionList . </blockquote>

So this line:

a := [3]int{369, 0, 963}

Is equivalent to this:

var a = [3]int{369, 0, 963}

But since the expression list is a <a href="https://golang.org/ref/spec#Composite_literals" rel="nofollow">composite literal</a> of type [3]int, the following is the same:

var a [3]int = [3]int{369, 0, 963}

<a href="https://golang.org/ref/spec#Variable_declarations" rel="nofollow">Spec: Variable declaration:</a>

<blockquote>

If a type is present, each variable is given that type. Otherwise, each variable is given the type of the corresponding initialization value in the assignment.

</blockquote>

So you may use any of the following, all declare and initialize a variable of type [3]int:

a := [3]int{369, 0, 963} b := [...]int{369, 0, 963} var c = [3]int{369, 0, 963} var d [3]int = [3]int{369, 0, 963} var e [3]int = [...]int{369, 0, 963} var f = [...]int{369, 0, 963}

<strong>Notes:</strong>

Note that in composite literals, it is valid to not list all values. Elements whose value is not explicitly specified will be the <a href="https://golang.org/ref/spec#The_zero_value" rel="nofollow">zero value</a> of the element type. You may include an optional index before a value in the enumeration to specify the element whose value it will be.

<a href="https://golang.org/ref/spec#Composite_literals" rel="nofollow">Spec: Composite literals:</a>

<blockquote>

For array and slice literals the following rules apply:

<ul><li>Each element has an associated integer index marking its position in the array.</li> <li>An element with a key uses the key as its index; the key must be a constant integer expression.</li> <li>An element without a key uses the previous element's index plus one. If the first element has no key, its index is zero.</li> </ul></blockquote>

Since your initial array contains a 0 which is the zero value for the element type int, you may exclude it from the literal. To create and initialize a variable to the value [3]int{369, 0, 963}, you may also do it like this:

// Value at index 1 implicitly gets 0: g := [3]int{369, 2: 963} h := [...]int{369, 2: 963}

Try all the examples on the <a href="https://play.golang.org/p/KgumxQDosZ" rel="nofollow">Go Playground</a>.

See this question for more details + practical examples: <strong><a href="https://stackoverflow.com/questions/36302441/keyed-items-in-golang-array-initialization" rel="nofollow">Keyed items in golang array initialization</a></strong>

Recommend

  • Paperclip in Production with Unicorn: uninitialized constant Paperclip (NameError)
  • Using Recaptcha with EPiServer XForms
  • Selecting TOP 4 records from multiple SQL Server tables. Using vb.net
  • C++ calling the default constructor with parens vs without parens [duplicate]
  • Is there anything like an Objective-C block in Java?
  • Laravel 5.3 blade decoding HTML Special Characters
  • Mysql stored procedure error
  • How can integers (keys & values) be added and retrieved from a NSDictionary
  • Call C++ class member function from C (Visual Studio)
  • gdb call function - how to use std::cout as parameter?
  • Color syntax highlighting in Bash while using R
  • How to startActivity for Result from Activity under Group Activity Under Tabs?
  • Cannot convert a char value to money. The char value has incorrect syntax
  • PHP Regex Look Around Help
  • Get XML response value with GDataXML
  • Use of qualified name in function parameter
  • What is Closure Compiler?
  • how to pass class attribute and value to markdown syntax
  • Syntax error near unexpected token 'elif'
  • SQL Server re-calculate or not?
  • How to access recipient on sent messages page with mailboxer
  • how to read a file in prolog?
  • Raphael.js function getBBox give back NAN/NAN/NAN in IE8
  • Getting syntax error in mysql-php. You have an error in your SQL syntax;
  • matching similar elements in between two lists
  • Swift Initialization Rule Confusion
  • ZipList with Scalaz
  • Use sed with regex and (
  • IE10 strips out hashtag from the URL
  • Create a link to a web page that runs a Javascript function on the page
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • C#: Import/Export Settings into/from a File
  • Cast between interfaces whose interface signatures are same
  • Question about instantiating object
  • Email verification using google app script and google forms
  • C++ Partial template specialization - design simplification
  • Javascript simulate pressing enter in input box
  • python regex in pyparsing
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Java static initializers and reflection