DEV Community

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

Posted on • Edited on

1

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free