Convert Figma logo to code with AI

josharian logoimpl

impl generates method stubs for implementing an interface.

1,016
89
1,016
5

Top Related Projects

Go tool to modify struct field tags

4,921

Automatically generate Go test boilerplate from your source code.

3,240

A stricter gofmt

Fast linters runner for Go

7,344

[mirror] Go Tools

Quick Overview

The josharian/impl repository is a Go package that provides a simple and lightweight implementation of the interface{} type in Go. It allows developers to create and work with dynamic, type-safe interfaces without the need for explicit type assertions or conversions.

Pros

  • Simplicity: The impl package provides a straightforward and easy-to-use interface for working with dynamic types in Go.
  • Type Safety: The package ensures type safety by providing a type-safe way to interact with interface{} values.
  • Flexibility: The impl package can be used in a variety of scenarios where dynamic typing is required, such as in data processing pipelines or plugin-based architectures.
  • Lightweight: The package is a small and dependency-free, making it easy to integrate into existing Go projects.

Cons

  • Limited Functionality: The impl package provides a limited set of functionality compared to more comprehensive dynamic typing solutions in Go, such as the reflect package.
  • Potential Performance Impact: The use of the impl package may have a slight performance impact compared to manual type assertions or the reflect package, especially for performance-critical applications.
  • Lack of Widespread Adoption: The impl package is not a widely used or well-known solution in the Go community, which may limit its adoption and support.
  • Potential Learning Curve: Developers unfamiliar with dynamic typing in Go may need to invest some time in understanding the impl package and how it differs from other approaches.

Code Examples

Here are a few examples of how to use the impl package in Go:

package main

import (
    "fmt"
    "github.com/josharian/impl"
)

func main() {
    // Create a dynamic value
    v := impl.New(42)
    fmt.Println(v.Get()) // Output: 42

    // Perform type-safe operations
    if i, ok := v.Int(); ok {
        fmt.Println(i * 2) // Output: 84
    }

    // Embed dynamic values in structs
    type Person struct {
        Name impl.Value
        Age  impl.Value
    }

    p := Person{
        Name: impl.New("Alice"),
        Age:  impl.New(30),
    }

    fmt.Println(p.Name.String()) // Output: Alice
    fmt.Println(p.Age.Int())     // Output: 30
}

Getting Started

To use the impl package in your Go project, follow these steps:

  1. Install the package using the Go package manager:
go get github.com/josharian/impl
  1. Import the package in your Go file:
import "github.com/josharian/impl"
  1. Create a new dynamic value using the impl.New() function:
v := impl.New(42)
  1. Perform type-safe operations on the dynamic value using the provided methods:
if i, ok := v.Int(); ok {
    fmt.Println(i * 2)
}
  1. Embed dynamic values in your own data structures:
type Person struct {
    Name impl.Value
    Age  impl.Value
}

p := Person{
    Name: impl.New("Alice"),
    Age:  impl.New(30),
}

That's the basic workflow for using the impl package in your Go projects. Refer to the package documentation for more advanced usage and examples.

Competitor Comparisons

Go tool to modify struct field tags

Pros of gomodifytags

  • Focuses on modifying struct field tags, offering more specialized functionality
  • Provides options for adding, removing, and updating specific tags
  • Supports custom formatting options for generated tags

Cons of gomodifytags

  • Limited to working with struct tags, less versatile than impl
  • Requires more specific input and configuration for tag modifications
  • May have a steeper learning curve for users unfamiliar with Go struct tags

Code Comparison

impl:

type F interface {
    Method(arg string) error
}

func (t *T) Method(arg string) error {
    panic("not implemented")
}

gomodifytags:

type User struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

// After modification
type User struct {
    Name string `json:"name" xml:"name"`
    Age  int    `json:"age" xml:"age"`
}

Summary

impl is a more general-purpose tool for generating method stubs, while gomodifytags specializes in manipulating struct tags. impl is better suited for interface implementation, whereas gomodifytags excels at fine-tuning struct field metadata. The choice between the two depends on the specific task at hand: method implementation or struct tag management.

4,921

Automatically generate Go test boilerplate from your source code.

Pros of gotests

  • Automatically generates table-driven tests for Go functions
  • Supports generating tests for entire files or specific functions
  • Offers customizable templates for test generation

Cons of gotests

  • Limited to generating tests only, doesn't assist with interface implementation
  • May generate unnecessary tests for simple functions
  • Requires manual adjustment of generated tests for complex scenarios

Code comparison

gotests:

func TestSum(t *testing.T) {
    tests := []struct {
        name string
        a    int
        b    int
        want int
    }{
        // TODO: Add test cases.
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := Sum(tt.a, tt.b); got != tt.want {
                t.Errorf("Sum() = %v, want %v", got, tt.want)
            }
        })
    }
}

impl:

type Calculator interface {
    Sum(a, b int) int
}

type calculator struct{}

func (c *calculator) Sum(a, b int) int {
    return a + b
}

Summary

gotests focuses on generating table-driven tests for Go functions, while impl helps implement interfaces. gotests is beneficial for quickly creating test structures, but may require manual adjustments. impl is more specialized for interface implementation but doesn't generate tests. Choose based on your specific development needs.

3,240

A stricter gofmt

Pros of gofumpt

  • More comprehensive code formatting, addressing additional style issues beyond standard gofmt
  • Stricter formatting rules for improved consistency across projects
  • Actively maintained with regular updates and improvements

Cons of gofumpt

  • May require adjustments to existing codebases to comply with stricter formatting rules
  • Potential for conflicts with other Go tools or IDE integrations that use standard gofmt

Code comparison

impl:

func (s *MyStruct) MethodName(arg1 Type1, arg2 Type2) (ReturnType1, error) {
    // Implementation
}

gofumpt:

func (s *MyStruct) MethodName(
    arg1 Type1,
    arg2 Type2,
) (ReturnType1, error) {
    // Implementation
}

Summary

impl is focused on generating method stubs for interfaces, while gofumpt is a stricter code formatter. impl is useful for quickly implementing interfaces, whereas gofumpt aims to improve code consistency and readability through additional formatting rules. The choice between the two depends on the specific needs of your project: interface implementation assistance or enhanced code formatting.

Fast linters runner for Go

Pros of golangci-lint

  • Comprehensive linting tool with multiple linters integrated
  • Highly configurable with options to enable/disable specific linters
  • Faster execution due to parallel processing of linters

Cons of golangci-lint

  • Larger and more complex tool, which may be overkill for simple projects
  • Steeper learning curve due to numerous configuration options
  • May produce more false positives or unnecessary warnings

Code comparison

impl:

//go:generate impl "s *Stack" push.Pusher

type Stack struct {
    // ...
}

golangci-lint:

linters:
  enable:
    - gofmt
    - golint
    - govet
  disable:
    - errcheck

Summary

impl is a focused tool for generating method stubs for interfaces, while golangci-lint is a comprehensive linting suite. impl is simpler and more straightforward for its specific use case, while golangci-lint offers a wide range of linting capabilities but with added complexity. The choice between the two depends on the project's needs: impl for quick interface implementations, golangci-lint for thorough code quality checks.

7,344

[mirror] Go Tools

Pros of tools

  • Comprehensive suite of development tools for Go
  • Officially maintained by the Go team, ensuring compatibility and regular updates
  • Includes a wide range of functionalities beyond interface implementation

Cons of tools

  • Larger and more complex, potentially overwhelming for simple tasks
  • May require more setup and configuration for specific use cases
  • Not focused solely on interface implementation, which might be less efficient for that specific task

Code comparison

impl:

package main

import "github.com/josharian/impl"

func main() {
    impl.Impl("f *File", "io.Reader", "Read")
}

tools:

package main

import "golang.org/x/tools/cmd/stringer"

func main() {
    stringer.Run("mytype")
}

Summary

While tools offers a comprehensive set of Go development utilities, impl focuses specifically on generating interface method stubs. tools provides broader functionality but may be more complex for simple tasks. impl is more straightforward for its specific use case but lacks the extensive features of tools. The choice between them depends on the developer's needs and the scope of the project.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

impl generates method stubs for implementing an interface.

go install github.com/josharian/impl@latest

Sample usage:

$ impl 'f *File' io.ReadWriteCloser
func (f *File) Read(p []byte) (n int, err error) {
	panic("not implemented")
}

func (f *File) Write(p []byte) (n int, err error) {
	panic("not implemented")
}

func (f *File) Close() error {
	panic("not implemented")
}

# You can also provide a full name by specifying the package path.
# This helps in cases where the interface can't be guessed
# just from the package name and interface name.
$ impl 's *Source' golang.org/x/oauth2.TokenSource
func (s *Source) Token() (*oauth2.Token, error) {
    panic("not implemented")
}

You can use impl from Vim with vim-go or vim-go-impl