/Golang

A repository containing code and projects developed while learning Go programming language.

Primary LanguageGo

Golang

Golang is a compiled language, which means that it is compiled to machine code before it is executed. Golang is a statically typed language, which means that the type of a variable is known at compile time.

Notes on Golang

Key Features

  • Concurrency: Golang has built-in support for concurrent programming with goroutines and channels.
  • Garbage Collection: Automatic memory management helps prevent memory leaks.
  • Standard Library: Extensive standard library with packages for various tasks like web development, cryptography, and I/O operations.
  • Cross-Platform: Golang can be compiled to run on multiple operating systems including Windows, macOS, and Linux.

Common Commands

  • go run <file.go>: Compile and run a Go program.
  • go build <file.go>: Compile a Go program and create an executable.
  • go test: Run tests in the current package.
  • go fmt: Format Go source code.

Basic Topics

  • Variables and Constants: Declaration and initialization of variables and constants.
  • Data Types: Understanding basic data types like int, float, string, and bool.
  • Control Structures: Using if-else, switch-case, and loops (for).
  • Functions: Defining and calling functions, understanding parameters and return values.
  • Error Handling: Using error type and handling errors in Go.

Intermediate Topics

  • Structs: Defining and using structs to create custom data types.
  • Methods: Associating functions with structs to define methods.
  • Interfaces: Defining and implementing interfaces for polymorphism.
  • Packages: Organizing code into packages and importing them.
  • Concurrency: Using goroutines and channels for concurrent programming.

Advanced Topics

  • Reflection: Using reflection to inspect types at runtime.
  • Generics: Understanding and using generics (available in Go 1.18+).
  • Testing: Writing unit tests and benchmarks using the testing package.
  • Profiling and Optimization: Profiling Go programs and optimizing performance.
  • Advanced Concurrency: Using sync package for advanced concurrency patterns.

Installation

To install Go, follow these steps:

  1. Download the Go installer from the official Go website.
  2. Run the installer and follow the on-screen instructions.
  3. Verify the installation by opening a terminal and typing go version.

Project Structure

The typical structure of a Go project is as follows:

/my-go-project
    /cmd
        /myapp
            main.go
    /pkg
        /mypackage
            mypackage.go
    /internal
        /myinternalpackage
            myinternalpackage.go
    /api
        api.go
    /web
        /static
        /templates
    go.mod
    go.sum
    README.md
  • cmd: Contains the entry points for the application.
  • pkg: Contains reusable packages.
  • internal: Contains packages that are only used within the project.
  • api: Contains API definitions and implementations.
  • web: Contains web-related files like static assets and templates.
  • go.mod: Defines the module and its dependencies.
  • go.sum: Contains the checksums of the dependencies.

Contributing

We welcome contributions to this project. To contribute, follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make your changes and commit them (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new Pull Request.

Please ensure that your code adheres to the project's coding standards and includes appropriate tests.