Skip to content

Instantly share code, notes, and snippets.

@monirz
Created October 5, 2017 09:49
Show Gist options
  • Select an option

  • Save monirz/687c738ffcc76fe8abe3f79c6517cd84 to your computer and use it in GitHub Desktop.

Select an option

Save monirz/687c738ffcc76fe8abe3f79c6517cd84 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
type SQLBuilder interface {
Select() SQLBuilder
Star() SQLBuilder
From(table string) SQLBuilder
Where(condition string) SQLBuilder
Build() string
}
type builder struct {
s string
}
func (b *builder) Select() SQLBuilder {
b.s = b.s + "SELECT "
return b
}
func (b *builder) Star() SQLBuilder {
b.s = b.s + " * "
return b
}
func (b *builder) From(table string) SQLBuilder {
b.s = b.s + " FROM " + table + " "
return b
}
func (b *builder) Where(condition string) SQLBuilder {
b.s = b.s + " WHERE " + condition + " "
return b
}
func (b *builder) Build() string {
return b.s
}
func main() {
b := &builder{}
s := b.Select().Star().From("foo")
fmt.Println(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment