Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/st0012/metago
metago is a package trying to provide Ruby-like meta-programming features to Go.
https://github.com/st0012/metago
go golang metaprogramming package
Last synced: 8 days ago
JSON representation
metago is a package trying to provide Ruby-like meta-programming features to Go.
- Host: GitHub
- URL: https://github.com/st0012/metago
- Owner: st0012
- License: mit
- Created: 2017-07-29T08:56:57.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-08-03T06:02:29.000Z (over 7 years ago)
- Last Synced: 2024-12-26T08:42:08.555Z (16 days ago)
- Topics: go, golang, metaprogramming, package
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 7
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# metago
[![Build Status](https://travis-ci.org/st0012/metago.svg?branch=master)](https://travis-ci.org/st0012/metago)
[![codecov](https://codecov.io/gh/st0012/metago/branch/master/graph/badge.svg)](https://codecov.io/gh/st0012/metago)
[![GoDoc](https://godoc.org/github.com/st0012/metago?status.svg)](https://godoc.org/github.com/st0012/metago)`metago` is trying to provide Ruby-like meta-programming features to Go. Mostly just for fun.
## Install
```
go get github.com/st0012/metago
```## Usage
Currently `metago` only has one function: `CallFunc`
```
CallFunc(receiver interface{}, methodName string, args ...interface{}) interface{}
```Here's how you can use it:
```go
package mainimport (
"fmt"
"github.com/st0012/metago"
)type Bar struct {
name string
}func (b *Bar) Name() string {
return b.name
}func (b *Bar) SetName(n string) {
b.name = n
}func (b *Bar) Send(methodName string, args ...interface{}) interface{} {
return metago.CallFunc(b, methodName, args...)
}func main() {
b := &Bar{}
b.Send("SetName", "Stan") // This is like Object#send in Ruby
fmt.Println(b.name) // Should be "Stan"
fmt.Println(b.Send("Name")) // Should also be "Stan"
}
```As you can see, you can call `*Bar` dynamically. Just like Ruby:
```ruby
b = Bar.new
b.send(:set_name, "Stan")
```## Future work
I'll trying to add more meta-programming features in Ruby like `method_missing` or `define_method`...etc.