DEV Community

Cover image for Learning GO: 06 - Function return values, if condition
Gaurav Vala
Gaurav Vala

Posted on • Edited on

Learning GO: 06 - Function return values, if condition

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

Different way to declare return Variables

  • when you declare the type of variables on function, there you can also add the variables name, and that way you dont have to declare those return variables
  • also then you dont have to add those variable names after the return statement, Go will understand which variables to return
func calculateFutureValue(investmentAmount, expectedReturn, years float64) (fv float64, rfv float64) {
    fv = (investmentAmount) * math.Pow(1+expectedReturn/100, float64(years))
    rfv = fv / math.Pow(1+inflationRate/100, years)
    return
}
Enter fullscreen mode Exit fullscreen mode
  • starting new project with command
go mod init example.com/bank
Enter fullscreen mode Exit fullscreen mode
  • Booleans for checks
wantsCheckBalance := choice == 1
Enter fullscreen mode Exit fullscreen mode
  • if condition is similar but only round parenthesis is not used, directly the condition is used
    if choice == 1 || choice == 2 {
    } 
Enter fullscreen mode Exit fullscreen mode
  • All the conditional && and || are also the same
  • to check the condition or compare we can use the double equals operator ==
  • else if is also the same as other language

    if choice == 1 {
        fmt.Println("Your Balance is", accountBalance)
    } else if choice == 2 {
  }
Enter fullscreen mode Exit fullscreen mode
  • Increment operator works the same
accountBalance += depositAmount
//which equals to accountBalance = accountBalance + depositAmount
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay