Learning GO: 03 - User Input, Variable Declaration without initialization
Hey! I am Currently learning Go Lang, and I am taking some basic Notes on my Notion and though I'd also just publish them here. They are not well thought out or well written but it's just me taking notes from time to time for my reference.
I am taking the Udemy Course by Maximilian Schwarzmüller,
Notes
User Input
Getting values from users through cli is pretty similar to how it is done in C language
we can use the Scan() method of the fmt package to do that.
fmt.Scan(&investmentAmount)
here the investmentAmount is already declared and it has a value in it so to override that we are using the “&” symbol, which is a pointer to the actual variable and here will override the value from user input
If user dont give any value to the variable, it will be initialized with the default value of the type, for float it will be 0.0
Declaring Variables without initial value
We can declare a variable without initializing any value to that variable, but for that, we will need to explicitly declare the type of the variable, which required as GO Lang is a statically typed language
This will be declared using the var keyword and then the variable name and then the type of variable
var years float64
var expectedReturn float64
All Methods to declare variables in GO Lang
const inflationRate = 2.5
var investmentAmount float64 = 1000
var years float64
expectedReturn := 5.5
Subscribe to my newsletter
Read articles from Gaurav Vala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Gaurav Vala
Gaurav Vala
I am a Front End Developer and Web Designer from Gujarat, India. Interested in working on applications that affect many people