Skip to content
This repository was archived by the owner on May 10, 2020. It is now read-only.

workshop-depot/gsrv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gsrv

Go Generic-Code Generator Server

intro

Watch here.

intro, text

The empty interface - interface{} - is used, in Go, to represent a value, that its type is unknown, at a certain point, in the code. Sometimes relying on empty interfaces, makes code hard to reason about and the type-safety provided by the compiler, would be of no use.

Since Go has not generics in its type system, the alternative approach is to use code generators, combined with copy/paste.

The problem with the copy/paste approach is, when you find and fix a bug in the original generic template or generic definition code, it's hard to find all the places that the code is copied to, and replace the fixed version.

This tool helps with that. In addition to keeping your generic definitions in sync, and applying changes automatically, by using blank imports, it ensures that all the code in the chain - from the main generic definition down the specialized generic implementation - is valid Go code. Because they get compiled by Go compiler!

get the tool

First go get the tool components:

$ go get -u github.com/dc0d/ggen
$ go get -u github.com/dc0d/gsrv

Then start the server:

$ gsrv

generate the skeleton

Then generate a skeleton for defining the generic type:

$ ggen create --name mygenericmap

Now go on and modify the skeleton and finalize the generic definition.

specialize the generic definition

Create a package and put a blank import of the generic definition inside it. Say we have intstrmap package, and inside intstrmap.go we add:

import (
    _ "mygenericmap"
)

Now this tool recognizes this blank import and copies the necessary files from the original generic definition, to this directory and keeps them in sync!

To specialize the original generic definition, we can not set the interface{}s, to concrete types. We can even specialize the implementation yet everything will remain synced!

To see it in action, watch the intro.

About

Go Generic-Code Generator Server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages