DEV Community

Cover image for Learning GO: 03 - User Input, Variable Declaration without initialization
Gaurav Vala
Gaurav Vala

Posted on • Edited on

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) 
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

All Methods to declare variables in GO Lang

const inflationRate = 2.5
var investmentAmount float64 = 1000
var years float64
expectedReturn := 5.5
Enter fullscreen mode Exit fullscreen mode

Top comments (0)