EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Go Programming Language

Fundamentals in Go

In programming, identifiers are used for naming purposes. Essentially, identifiers are user-defined names for program components. In the Go language, identifiers can represent variable names, function names, constants, statement labels, package names, or types.

Example:

package main
import "fmt"

func main() {
    var course = "Learn Go Programming"
}

In the example above, there are three identifiers:

  • main: The name of the package.
  • main: The name of the function.
  • course: The name of the variable.

Rules for Defining Identifiers:

To define valid Go identifiers, certain rules must be followed. Failing to adhere to these rules results in compile-time errors:

  1. The identifier name must start with a letter or an underscore (_). It may include letters (a-zA-Z), digits (0-9), and underscores (_).
  2. The identifier name cannot start with a digit.
  3. Identifiers are case-sensitive.
  4. Reserved keywords cannot be used as identifier names.
  5. While there is no maximum limit on identifier length, it is recommended to keep names between 4 and 15 characters for readability.

Example:

// Valid identifiers:
_student23
course
course2023
CourseName
COURSE_NAME

// Invalid identifiers:
123course
func
package

Predeclared Constants:
truefalseiotanil

Predeclared Types:
intint8int16int32int64uintuint8uint16uint32uint64uintptrfloat32float64complex128complex64boolbyterunestringerror

Predeclared Functions:
makelencapnewappendcopyclosedeletecomplexrealimagpanicrecover

  • Blank Identifier (_): The underscore character _ is a special identifier used as an anonymous placeholder in declarations, assignments, or operands.
  • Exported Identifiers: An identifier is considered exported if it starts with an uppercase letter and is declared in the package block or as a variable, function, type, or method name within a package. These identifiers can be accessed outside the package.

Example of Exported Identifiers:

File: utilities.go

// utilities.go

package utilities

// Exported variable
var GreetingMessage = "Welcome to Go Utilities!"

File: main.go

// main.go

package main

import (
    "fmt"
    "path/to/yourproject/utilities"
)

// Exported variable
var ApplicationName = "Go Identifier Tutorial"

func main() {
    // Accessing exported identifier within the same file
    fmt.Println(ApplicationName)

    // Accessing exported identifier from another package
    fmt.Println(utilities.GreetingMessage)
}

Output:

Go Identifier Tutorial
Welcome to Go Utilities!

Keywords or Reserved words are the words in a programming language that serve specific purposes or represent predefined functionalities. These words are restricted from being used as identifiers, such as variable names or function names. Using these reserved words as identifiers will lead to compile-time errors.

Example:

// Go program to demonstrate the use of keywords
package main

import "fmt"

// Here, package, import, func, var are keywords
func main() {

    // Using a valid identifier
    var message = "Hello, Go Language!"

    fmt.Println(message)

    // Attempting to use a reserved word as an identifier
    // will result in a compile-time error
    // var return = "Invalid Identifier"
}

Output:

Hello, Go Language!

Go language contains 25 keywords in total, as listed below:

break       case       chan      const       continue
default     defer      else      fallthrough for
func        go         goto      if          import
interface   map        package   range       return
select      struct     switch    type        var

Example:

// Go program demonstrating the usage of keywords

// The package keyword is used to specify the main package
package main

// The import keyword is used to include the "fmt" package
import "fmt"

// The func keyword is used to define a function
func main() {

    // The var keyword is used to declare variables
    // These are valid identifiers: name, lang, and topic
    var name = "Learn Go Programming"
    var lang = "Go Language"
    var topic = "Keywords"

    fmt.Printf("Learning Platform: %s", name)
    fmt.Printf("\nProgramming Language: %s", lang)
    fmt.Printf("\nTopic: %s", topic)
}

Output:

Learning Platform: Learn Go Programming
Programming Language: Go Language
Topic: Keywords
End of lesson.