Go

From G1Joshi/Agent-Skills · View interactive version

Go is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Go programming assistance covering goroutines, channels, interfaces, generics, context, and error handling. Use when building microservices, high-concurrency network servers, or cloud-native infrastructure.

How to use this skill

Skill file

Go

A simple, fast, and concurrent language developed by Google.

When to Use

Quick Start

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")

    ch := make(chan string)
    go func() {
        ch <- "from goroutine"
    }()

    msg := <-ch
    fmt.Println(msg)
}

Core Concepts

Goroutines & Channels (CSP Concurrency)

Lightweight green threads managed by the Go runtime scheduler, communicating via channels:

package main

import (
    "fmt"
    "time"
)

func fetchMetric(id int, ch chan<- string) {
    time.Sleep(100 * time.Millisecond)
    ch <- fmt.Sprintf("Metric %d: 99.4", id)
}

func main() {
    ch := make(chan string, 3)

    for i := 1; i <= 3; i++ {
        go fetchMetric(i, ch)
    }

    for i := 1; i <= 3; i++ {
        fmt.Println(<-ch)
    }
}

Implicit Interface Implementation

Types satisfy interfaces automatically without declaring implements:

type Reader interface {
    Read(p []byte) (n int, err error)
}

// Any struct with a matching Read() method satisfies Reader implicitly

Explicit Error Handling & Sentinel Errors

Errors are regular values returned explicitly as the last return argument:

import (
    "errors"
    "fmt"
)

var ErrNotFound = errors.New("record not found")

func FindUser(id string) (*User, error) {
    if id == "" {
        return nil, fmt.Errorf("invalid query: %w", ErrNotFound)
    }
    return &User{ID: id}, nil
}

Common Patterns

Worker Pool with Context Cancellation

Problem: Processing thousands of jobs concurrently without overwhelming system resources or memory.

Solution:

Use fixed worker goroutines reading from a shared channel with context cancellation:

package main

import (
	"context"
	"fmt"
	"sync"
)

func worker(ctx context.Context, id int, jobs <-chan int, results chan<- int, wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		select {
		case <-ctx.Done():
			return
		case job, ok := <-jobs:
			if !ok {
				return
			}
			results <- job * 2
		}
	}
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------------------------------ | :---------------------------------------------------------------------- | :--------------------------------------------------------------------------- |

| fatal error: all goroutines are asleep - deadlock! | Goroutine waiting on channel with no senders or mutex locked twice. | Ensure channel close or buffer space, and verify mutex release in defer. |

| panic: runtime error: invalid memory address or nil pointer dereference | Accessing method or field on nil pointer variable. | Check pointer with if ptr != nil before dereferencing. |

| race detected during execution of test | Concurrent reads and writes to shared variable without synchronization. | Run with go test -race and protect accesses with sync.Mutex or channels. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.