Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active December 20, 2024 10:45
Show Gist options
  • Save Integralist/4c4983e9da327cb83a0e9c8b90396ac0 to your computer and use it in GitHub Desktop.
Save Integralist/4c4983e9da327cb83a0e9c8b90396ac0 to your computer and use it in GitHub Desktop.
[Programming Terminology] #programming #terminology

Programming Terminology in Go

1. Expression

  • Definition: A combination of values, variables, operators, and function calls that evaluates to a single value.
  • Examples:
    • 2 + 2 (evaluates to 4)
    • x * y (evaluates to the product of x and y)
    • time.Second * 10 (evaluates to a time.Duration value of 10 seconds)
  • Usage: Expressions are used to compute values. They can appear within statements, assignments, or function arguments.

2. Statement

  • Definition: A complete unit of execution that performs an action, like assigning a value, calling a function, or controlling the flow of a program.
  • Examples:
    • Assignment: x := 5
    • Function call: fmt.Println("Hello, World!")
    • Control flow: if x > 10 { fmt.Println("x is large") }
  • Types of Statements:
    • Expression Statement: An expression used as a standalone statement (e.g., x + y is an expression; x + y; on its own would be invalid in Go, but fmt.Println(x + y) is a valid statement).
    • Control Statement: Includes if, for, switch, etc.
    • Declaration Statement: Declares variables or constants, e.g., var a int.

3. Directive

  • Definition: A special instruction to the compiler or tooling, modifying behavior at compile time or runtime.
  • Examples:
    • //go:generate: Tells the Go toolchain to generate code during build processes.
    • Build constraints: // +build linux ensures code is only compiled on Linux systems.
  • Usage: Directives are typically comments prefixed with //, interpreted by Go tools.

4. Declaration

  • Definition: Introduces new identifiers (variables, constants, types, or functions) into the program.
  • Examples:
    • Variable declaration: var x int
    • Constant declaration: const Pi = 3.14
    • Type declaration: type Point struct { X, Y int }
    • Function declaration: func Add(a, b int) int { return a + b }

5. Keyword

  • Definition: Reserved words in a language with predefined meanings that cannot be used as identifiers.
  • Examples:
    • if, else, func, return, var, const, etc.
  • Usage: Keywords form the syntax and structure of the language.

6. Block

  • Definition: A sequence of statements enclosed in curly braces {}.
  • Examples:
    func main() {
        fmt.Println("Hello, World!") // This is a block
    }
  • Usage: Blocks are used to group statements together in functions, loops, or conditionals.

7. Literal

  • Definition: A fixed value written directly in the code.
  • Examples:
    • Numbers: 42, 3.14
    • Strings: "hello"
    • Booleans: true, false
  • Usage: Literals are used to represent constant values in a program.

8. Operator

  • Definition: A symbol that performs operations on variables or values.
  • Examples:
    • Arithmetic: +, -, *, /
    • Logical: &&, ||, !
    • Relational: ==, !=, <, >
  • Usage: Operators are used within expressions to compute values or make comparisons.

9. Type

  • Definition: A classification that defines the kind of data a variable or value can hold.
  • Examples:
    • Primitive types: int, float64, string
    • Composite types: struct, array, slice, map
  • Usage: Types ensure variables are used consistently and define the operations applicable to them.

10. Function

  • Definition: A block of code that performs a specific task and can be reused.
  • Examples:
    func Add(a, b int) int {
        return a + b
    }
  • Usage: Functions encapsulate logic and make the code reusable and modular.

11. Package

  • Definition: A collection of related Go files grouped together to provide reusable functionality.
  • Examples:
    • Standard packages: fmt, time, strings
    • Custom packages: mypackage
  • Usage: Packages allow modular programming and reuse of code across projects.

12. Import

  • Definition: Brings a package into the current file for use.
  • Examples:
    import "fmt"
  • Usage: Imports allow access to external or standard library functionality.

13. Comment

  • Definition: A human-readable note in the code that is ignored by the compiler.
  • Examples:
    • Single-line: // This is a comment
    • Multi-line: /* This is a multi-line comment */
  • Usage: Used for documentation or explanations.

Example Code with All Terms

// This is a package declaration (declaration)
package main

import "fmt" // Import statement

// Function declaration
func main() {
    var x int = 10 // Declaration statement
    const y = 20   // Constant declaration

    // Expression within an assignment statement
    result := x + y

    // Control flow statement
    if result > 15 {
        fmt.Println("Result is greater than 15") // Expression statement
    }

    // Function call within a block
    fmt.Println("End of program") // Directive example: //go:generate could appear here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment