Declaring and Initializing go variables

Warning! I am not a pro writer. So today I am going to talk about declaring and initializing variables in Go. the way to declare variables on go is as follows. according to w3 schools this is the syntax.

var variablename type = value

an example of code declaring a variable would be first get in the terminal and go to your folder of stuff. I just made a new directory called variables then cd into it: I then touch variables.go, then I just open my text editor there.

var name_example string = "This is an example string"

that up there is an example of how to declare a variable initialized. the name_string is the actual name of the string of type string and the value is “this is an example string”

Another way to declare and initialize variables are without the string key word string an example piece of code would be in the below example in which the type is inferred.

var santiCode = "Heck Yeah"

Another way to declare variables is like this: this way to declare

x := 2 //type is inferred

You can also declare variables without initializing but you have to use the var keyword.

 var a string
 var b int
 var c bool

A variable is nothing more than a place that holds information in the memory of the computer.

Here are the Diferrences between var and :=

var:=
Can be used inside and outside of functionsCan only be used inside functions
Variable declaration and value assignment can be done separatelyVariable declaration and value assignment cannot be done separately (must be done in the same line)

looking at the differences play with the code and make the computer give you errors, this going to be for me in my opinion the best way to learn when not to use the := and the var word.

References https://www.w3schools.com/go/go_variables.php

You can also do Multiple Variable Declarations the example from w3 schools is as follows:

var a, b, c, d int = 1, 3, 5, 7

my example would be something like

var variable1, variable2, variable3, variable4 int = 1, 2, 3, 4

in my example I am declaring four variables starting with the var keyword follow by the variable name, variable1, variable2, variable3, variable4 followed by the int keyword that means that the values would be of type integer and the values for variable1 is 1, variable2 is 2, variable3 is 3, and variable4 is 4. I figured If I break it down like it might be easy to learn for someone that it is not used to read code and understand variables. in my example above if you use the type int keyword you have to used only that type another example with out the type keyword would be

var myName, myMotivationNum = "Santi", 110

yourName, yourMotivation := "Unknown", 100

You can also declare variables inside a block. looking at the example from w3 Schools you can do something like:

For naming conventions you can read here about it, although pretty much universal if you know some of the other languages.

Resources: https://gobyexample.com/variables

https://golangbot.com/variables/

https://www.golangprograms.com/go-language/variables.html

https://zetcode.com/golang/variable/

https://go.dev/tour/list

https://www.tutorialspoint.com/go/go_variables.htm

https://www.geeksforgeeks.org/go-variables/

This is all for now!

Very Respectfully,

Santi

PEACE!!

One thought on “Declaring and Initializing go variables

Comments are closed.