Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2369

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T04:45:08+00:00 2024-11-26T04:45:08+00:00

Working with variables and conditionals in Go Lang

  • 61k

I recently started to learn Go Lang, and as part of this I have been learning about variables and how they differ to variables in other languages I have worked with such as JavaScript and PHP.

Getting to grips with variables

In Go, variables are explicitly declared and when you compile your application, Go will infer the type of the initialized variables and use this to check the type-correctness of your code.

To define a variable in Go Lang we use var followed by the name of the variable, equals, and the value we want to assign to the variable. We can update our ‘Hello World’ application from earlier to use variables for each of the words.

The first step is to create a new file, in this case, I used using-variables.go. We can then take the original code from our hello world application and create 2 variables, one for each of the words. We then update our fmt.Println to instead join the strings, adding a space in between.

package main  import "fmt"  func main() {     var hello = "hello"     var world = "world"      fmt.Println(hello + " " + world) } 
Enter fullscreen mode Exit fullscreen mode

We can then run this example:

go run 02-using-variables.go 
Enter fullscreen mode Exit fullscreen mode

This will result in the words hello world being logged to the console. While this is a fairly contrived example, is shows us how we can easily update code which previously hard coded to now be customisable.

Introducing conditional logic to our application

The next thing we are going to learn about is how we can introduce conditional logic to our hello world application. In this case I want to allow the user to have the option to provide a name and instead of saying hello to the world we will say hello to that individual person instead 😀.

The first step is to create our new file for the new version of the application. I have called my file conditional-logic.go and as a starting point I copied and pasted the code from our previous application.

We now need to allow our application to get user input. To do this we need to import the os package which will make the arguments available to us as an Array. To access this Array we use os.Args, the first item of which will be the path to our application and the rest of the values will be the arguments we passed to it.

We only want to set the name when an argument has been passed, to do this we can use an if statement. In this if statement we want to check that the length of the arguments, excluding the application path is equal to one, we do this with the following.

if len(os.Args[1:]) == 1 {     name = os.Args[1] } 
Enter fullscreen mode Exit fullscreen mode

If we incorporate this into our completed code it will look as follows:

 package main  import (   "fmt"   "os" )  func main() {   var hello = "hello"   var name = "world"    if len(os.Args[1:]) == 1 {     name = os.Args[1]   }    fmt.Println(hello + " " + name) } 
Enter fullscreen mode Exit fullscreen mode

To run our application we can simply run the command:

go run conditional-logic.go 
Enter fullscreen mode Exit fullscreen mode

This will still output hello world however if we now pass a name as an additional argument:

go run conditional-logic.go jonathan 
Enter fullscreen mode Exit fullscreen mode

The output will now be hello jonathan, this is exactly what we wanted to achieve 🤗.

Wrapping up

Thank you for taking the time to read about using variables in Go Lang, it was good to start playing with variables and conditionals and I can definitely see similarities to how I would write the same thing in JavaScript.

codenewbiegowebdev
  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.