Go Variable Naming Rules (2024)

Go Variable Naming Rules

A variable can have a short name (like x and y) or a more descriptive name (age, price, carname, etc.).

Go variable naming rules:

  • A variable name must start with a letter or an underscore character (_)
  • A variable name cannot start with a digit
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • There is no limit on the length of the variable name
  • A variable name cannot contain spaces
  • The variable name cannot be any Go keywords

Multi-Word Variable Names

Variable names with more than one word can be difficult to read.

There are several techniques you can use to make them more readable:

Camel Case

Each word, except the first, starts with a capital letter:

myVariableName = "John"

Pascal Case

Each word starts with a capital letter:

MyVariableName = "John"

Snake Case

Each word is separated by an underscore character:

my_variable_name = "John"


W3schools Pathfinder

Track your progress - it's free!

Go Variable Naming Rules (2024)

FAQs

Go Variable Naming Rules? ›

A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables) There is no limit on the length of the variable name.

What are the rules for variable names in Golang? ›

The naming of variables is quite flexible, but there are some rules to keep in mind:
  • Variable names must only be one word (as in no spaces).
  • Variable names must be made up of only letters, numbers, and underscores ( _ ).
  • Variable names cannot begin with a number.
May 29, 2019

What are the 5 rules to declare variable name? ›

Variable Names
  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

What is the naming convention for Go package? ›

Go package names should be short and contain only lowercase letters. A package name composed of multiple words should be left unbroken in all lowercase. For example, the package tabwriter is not named tabWriter , TabWriter , or tab_writer .

Is Golang using Snakecase or camelCase? ›

Golang is one of the few that has stepped back and doesn't use snake_case and uses camelCase instead.

What is not allowed in a variable name? ›

Specifically, spaces are not permitted in the variable names, as variable name must be a single word. Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name.

Why does Golang use short variable names? ›

But if you read it carefully, they said they prefer those short names especially for local variables with limited scope. You don't need to go through the whole code to find their meaning, just through the current scope/code block. Variable names in Go should be short rather than long.

What are 3 rules for naming variables? ›

6 Rules for Naming Variables in Programming
  • Variable name must begin with a letter, dollar sign ($) or an underscore (_): ...
  • What you cannot start a variable name with: ...
  • You cannot use reserve words to name variables: ...
  • Variable are case sensitive: ...
  • Use variable names that describe the value:
Dec 3, 2021

What are the six rules for names of variables? ›

Variable Names
  • A variable name must start with a letter or the underscore character.
  • A variable name cannot start with a number.
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

How do you properly name a variable? ›

  1. Use Intention-Revealing Names. The name of the variable, function, class, etc should be sufficient enough to understand its purpose. ...
  2. Name Functions as Verbs. ...
  3. Name Classes as Nouns. ...
  4. Use Meaningful Distinction. ...
  5. Use Pronounceable Names. ...
  6. Use Searchable Names. ...
  7. Avoid Encodings.

What is the underscore in Golang variable name? ›

_(underscore) in Golang is known as the Blank Identifier. Identifiers are the user-defined name of the program components used for the identification purpose. Golang has a special feature to define and use the unused variable using Blank Identifier.

What is the difference between Go module and package name? ›

Package vs Module

A package is a directory of .go files, and it is the basic building block of a Go program. Packages help to organize code into reusable components. On the other side, a module is a collection of packages with built-in dependencies and versioning. A module comes with two additional files go.

What is the naming convention for ViewModel? ›

Naming Conventions for Name of a Type

“ViewModel” is simply added to a “Page”-suffixed name to yield the name of its ViewModel. However, only “Model” is added to a “View”-suffixed name to yield the name of its companion ViewModel.

What are the conventions for naming variables? ›

The choice of a variable name should be mnemonic — that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

What characters are allowed in variable names? ›

Follow these rules when naming a symbolic variable:
  • The first character must be one of the following: A-Z, (a-z), _, #, $, @. ...
  • The remaining characters can be any of the above, and 0 through 9.
  • The variable name can be up to 252 characters in length (not counting the ampersand).

What are 4 rules for naming variables in Visual Basic? ›

Use the following rules when you name procedures, constants, variables, and arguments in a Visual Basic module:
  • You must use a letter as the first character.
  • You can't use a space, period (.), exclamation mark (!), or the characters @, &, $, # in the name.
  • Name can't exceed 255 characters in length.
Sep 13, 2021

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 5834

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.