-
-
Save skryukov/d1c501dae2f7ee274fcd231924de3b65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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