Eo

Eo provides syntactic sugars for C++ developers to write Go-like code.

Eo is not recommended for peformance-critical use case. This aims at migrating existing codebase from Go to C++ with minimum changes.

Eo is under active development and its APIs are not stable.

Eo means “I go” in Latin and its pronunciation is “Ay-Oh” like what Freddie Mercury shouted at Live Aid.

License: MIT

Requirements

Build dependencies will be installed by Conan package manager.

  • C++20 (GCC 11, Clang 10)
  • Boost 1.78 (On MacOS, use brew to install boost)
  • fmt
  • scope-lite

Usage

For more examples, refer here.

Goroutine

Goroutine is emulated by C++20 stackless coroutine and awaitable of Boost.Asio.

func<R = void> is an alias type of boost::asio::awaitable<R>.

To generate awaitable function, co_await or co_return keyword should be in function body.

// Go
func f(s string) {
  fmt.Println(s)
}

func main() {
  go f("hello")
  go func() {
    fmt.Println("world")
  }()
}

// C++
func<> f(std::string s) {
  fmt::println(s);
  co_return; // if co_await keyword is used in function body, this can be omitted
}

func<> eo_main() {
  go(f("hello"));
  go([]() -> func<> {
    fmt::println("world");
    co_return;
  });
  co_return;
}

Channel

Channel is emulated by concurrent_channel of Boost.Asio.

Receive operator <-ch and send operator ch <- are replaced by * and << operators.

// Go
func main() {
  ch := make(chan string)
  go func() { ch <- "ping" }()
  msg := <-ch
  fmt.Println(msg);
}

// C++
func<> eo_main() {
  auto ch = make_chan<std::string>();
  go([&]() -> func<> { co_await (ch << "ping"); });
  auto msg = co_await *ch; // * operator calls async_receive(), so co_await is necessary
  fmt::println(msg);
}

Select statement

Select statement is emulated by awaitable_operators of Boost.Asio.

This has most different syntax from that of Go.

// Go
func f() {
  for {
    select {
    case msg := <-ch:
      fmt.Println(msg)
    default:
      return
    }
  }
}

// C++
func<> f() {
  for (;;) {
    // co_await awaitable functions combined by || operator will return
    // std::variant<...> of possible return values from awaitable functions.
    // Returned variant will have first returned value from awaitable function executions.
    // default_chan is a closed channel and it returns immediately so as to work like default case.
    auto res = co_await (*ch || *default_chan);
    switch (res.index()) {
    case 0: // *ch
      fmt::println(std::get<0>(res));
      break;
    case 1: // *default_chan
      co_return;
    }
  }
}

Defer

Defer is emulated by scope-lite that implements experimental std::scope_exit.

For convenience, temporary variable for assigning scope_exit instance is generated by eo_defer macro.

// Go
func f() {
  defer fmt.Println("world")
  fmt.Println("hello")
}

// C++
func<> f() {
  eo_defer([]() { fmt::println("world"); });
  fmt::println("hello");
}

Libraries

Frequently used Go APIs are emulated, but their behaviors are not completely same.

For example, fmt of Go is emulated by fmt of C++, but its behavior follows original C++ API, not that of Go.

GitHub

View Github