Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types in Go
  • Go Keywords
  • Go Control Flow
  • Go Functions
  • GoLang Structures
  • GoLang Arrays
  • GoLang Strings
  • GoLang Pointers
  • GoLang Interface
  • GoLang Concurrency
Open In App
Next Article:
Difference between var keyword and short declaration operator in Golang
Next article icon

Difference between var keyword and short declaration operator in Golang

Last Updated : 11 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

A variable is a storage location or place holder used for holding the value. It allows us to manipulate and retrieve the stored information. There are two ways to declare the variables in Golan which are as follows:

  • var varName type = value
  • varName := value

Difference between var keyword and short declaration operator

var keywordshort declaration operator
var is a lexical keyword present in Golang.:= is known as the short declaration operator.
It is used to declare and initialize the variables inside and outside the functions.It is used to declare and initialize the variables only inside the functions.
Using this, variables have generally package level or global level scope. It can also have local scope.Here, variables has only local scope as they are only declared inside functions.
Declaration and initialization of the variables can be done separately.Declaration and initialization of the variables must be done at the same time.
It is mandatory to put type along with the variable declaration.There is no need to put type. If you, it will give error.

Example 1: In this program you can see the myvariable1 is declared using var keyword and it has local scope. myvariable2 is also declared using var keyword along with type int but there is no initialization has been done. So it will take the default value of int type i.e. zero(you can see in the output). myvariable3 is declared and initialized using short variable declaration operator and it has local scope. 

Go
// Go program to show the use of var lexical  // keyword and short declaration operator package main  import (     "fmt"; )  func main() {  // using var keyword to declare  // and initialize the variable var myvariable1 = 100  fmt.Println(myvariable1)  // using var keyword to declare  // the variable along with type // type is mandatory while declaration var myvariable2 int  fmt.Println(myvariable2)  // using short variable declaration myvariable3 := 200  fmt.Println(myvariable3)      } 

Output:

100 0 200

Example 2: In this program you can see the myvariable1 is declared using var keyword and it has global scope. myvariable2 is also declared using var keyword along with type int but there is no initialization has been done. So it will take the default value of int type i.e. zero(you can see in the output). myvariable3 is declared and initialized using short variable declaration operator and it has local scope. 

Go
// Go program to show the use of var lexical  // keyword and short declaration operator package main  import (     "fmt" )  // using var keyword to declare  // and initialize the variable // it is package or you can say  // global level scope var myvariable1 = 100  func main() {  // accessing myvariable1 inside the function fmt.Println(myvariable1)  // using var keyword to declare  // the variable along with type var myvariable2 int  fmt.Println(myvariable2)  // using short variable declaration myvariable3 := 200  fmt.Println(myvariable3)      } 

Output:

100 0 200

Example 3: In this program you can see the myvariable1 is declared using var keyword and it has global scope. myvariable2 is also declared using var keyword along with type int but there is no initialization has been done. So it will take the default value of int type i.e. zero(you can see in the output). myvariable3 is declared and initialized using short variable declaration operator outside the function which is not allowed and hence gives error. 

Go
// Go program to show the use of var lexical  // keyword and short declaration operator package main  import (     "fmt" )  // using var keyword to declare  // and initialize the variable // it is package or you can say  // global level scope var myvariable1 = 100  // using short variable declaration // it will give an error as it is not  // allowed outside the function myvariable3 := 200  func main() {  // accessing myvariable1 inside the function fmt.Println(myvariable1)  // using var keyword to declare  // the variable along with type var myvariable2 int  fmt.Println(myvariable2)   fmt.Println(myvariable3)      } 

Error:

./prog.go:18:1: syntax error: non-declaration statement outside function body


Next Article
Difference between var keyword and short declaration operator in Golang

A

anshul_aggarwal
Improve
Article Tags :
  • Go Language
  • Go-Keywords
  • Go-Operators
  • Golang

Similar Reads

    Difference Between Golang and Dart
    Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports the
    2 min read
    Short Variable Declaration Operator(:=) in Go
    Short Variable Declaration Operator(:=) in Golang is used to create the variables having a proper name and initial value. The main purpose of using this operator to declare and initialize the local variables inside the functions and to narrowing the scope of the variables. The type of the variable i
    7 min read
    Difference Between Golang and PHP
    Golang is a statically typed, compiled programming language invented at Google headquarter by Mr. Robert Griesemer, Mr Rob Pike, and Mr. Ken Thompson. Its development began in 2007 and it was made available to the public in the year 2009. It's open-source and freely available for public use. It is m
    2 min read
    Difference Between Golang and Ruby
    Before stepping into a new project software developing team goes through severe discussions in order to choose the best language for their project. As we are aware that different technologies have their pros and cons and similarly the technology which is looking vibrant for one project might be lack
    4 min read
    Different Ways to Convert an Integer Variable to String in Golang
    Integer variable cannot be directly convert into String variable. In order to convert string to integer type in Golang , you have store value of integer variable as string in string variable. For this, we are using strconv and fmt package functions. 1. Itoa() Function:  The Itoa stands for Integer t
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences