Skip to content

Instantly share code, notes, and snippets.

@skryukov
Created October 25, 2021 09:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skryukov/d1c501dae2f7ee274fcd231924de3b65 to your computer and use it in GitHub Desktop.
Save skryukov/d1c501dae2f7ee274fcd231924de3b65 to your computer and use it in GitHub Desktop.
# Golang DSL
class << self
def package(pkg)
mod_name = pkg.to_s.capitalize
unless GoRuby.const_defined?(mod_name)
GoRuby.module_eval("#{mod_name} = Module.new { extend self }")
end
@__go_package__ = GoRuby.const_get(mod_name)
end
def import(pkg)
mod_name = pkg.split('_').collect(&:capitalize).join
raise "unknown package #{pkg}" unless GoRuby.const_defined?(mod_name)
define_method(pkg) { GoRuby.const_get(mod_name) }
end
def func(attrs)
current_package = @__go_package__
name, block = attrs
if current_package.respond_to? name
raise "#{name} already defined for package #{current_package}"
end
current_package.module_eval { define_method(name, block) }
end
def method_missing(name, *_args, &block)
if block
[name, block.to_proc]
else
name
end
end
end
# the Golang standard library
module GoRuby
module Fmt
class << self
def Println(*attrs)
str = "#{attrs.join(' ')}\n"
$stdout << str
[str.bytesize, nil]
end
end
end
end
# Call main() function
at_exit do
GoRuby::Main.main
end
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment