Golang developers should try Odin

Golang developers should try Odin

Golang 开发者应该尝试一下 Odin

Introduction I’ve been using Golang for 10 years, and even though I tried other programming languages, I always went back to it because of its simplicity. It is so simple that reading the source code and playing around helped me pick up the majority of the syntax when I first got into Golang. 引言 我使用 Golang 已经 10 年了,尽管我也尝试过其他编程语言,但我总是因为它的简洁性而回归。它非常简单,以至于我刚接触 Golang 时,通过阅读源代码和动手实践,就掌握了大部分语法。

However, it pains me to say that Golang has problems because of its garbage collector and runtime. Some of the problem are listed here: CGO, it runs and compiles slowly, you have to know and write C in comments!! It can not compile to WASM, the language is slow compared to Rust, it is easy to have memory leaks and they are hard to find, and the error type is not good. But even with these problems, I still prefer Golang to other programming languages, until I met Odin a week ago, and it was love at first sight. This article will show you the beauty of Odin. 然而,我很痛苦地说,由于垃圾回收器和运行时的存在,Golang 存在一些问题。其中一些问题包括:CGO、运行和编译缓慢、你必须了解并要在注释中编写 C 代码!!它无法编译为 WASM,与 Rust 相比语言运行速度较慢,容易出现且难以发现内存泄漏,以及错误类型设计不佳。但即便有这些问题,我仍然比其他语言更偏爱 Golang,直到一周前我遇见了 Odin,那是一见钟情。本文将向你展示 Odin 的魅力。

What is Odin and who is it for Odin is a new programming language that is 60% Golang, 20% features that you wished Golang had, 10% array programming, and 10% low level, FFI, memory management. People think that it is only for game development just because it includes 3D libraries with the compiler, but I disagree. Odin is also for backend development because it has a core library similar to Go’s standard library. Which means that it has all the building blocks for us to copy the rest of Go’s libraries like Chinese manufacturers. 什么是 Odin 以及它适合谁 Odin 是一门新的编程语言,它由 60% 的 Golang 特性、20% 你希望 Golang 拥有的功能、10% 的数组编程以及 10% 的底层、FFI 和内存管理组成。人们认为它只适用于游戏开发,仅仅因为它在编译器中包含了 3D 库,但我不同意。Odin 也适用于后端开发,因为它拥有类似于 Go 标准库的核心库。这意味着它具备了所有基础模块,让我们能够像中国制造商那样“复刻”其余的 Go 库。

I know that some of you play around with Rust, Zig, C3 or Hare, but these do not have array programming and struct tags in their languages. Just ask yourself: how will you serialize data between DBs and markup languages without struct tags? how will you write a server side anticheat without array programming? Don’t you want to make linux gamers happy? It even has quaternions! Do you know what that is? Me neither! But I am excited to learn. 我知道你们中有些人玩过 Rust、Zig、C3 或 Hare,但这些语言中没有数组编程和结构体标签(struct tags)。问问自己:没有结构体标签,你如何在数据库和标记语言之间序列化数据?没有数组编程,你如何编写服务器端的反作弊系统?难道你不想让 Linux 玩家开心吗?它甚至还有四元数(quaternions)!你知道那是什么吗?我也不知道!但我很兴奋去学习它。

Odin is ready The language will not change and it will stay stable until you retire. However, the compiler, toolchain, and core library are still in development and always improved. Also, it is used in production from JangaFX and ChiAha™. Odin 已经准备就绪 这门语言不会发生变动,并且在你退休之前它都将保持稳定。不过,编译器、工具链和核心库仍在开发中,并不断改进。此外,它已被 JangaFX 和 ChiAha™ 用于生产环境。

The characteristics of Odin The language has 31 keywords, but each keyword may have a #directive or @(attribute). It may seem too much for someone that cannot remember more than 26 keywords like me, but all the keywords, directives, and attributes connect so seamlessly together like reading English. For example, the switch-case that works like in Golang requires from the user to specify every case, unless you put the #partial directive in front of it. It will really make sense to you how easy it is for you to understand the code without knowing the language. Odin 的特性 这门语言有 31 个关键字,但每个关键字都可能带有 #指令(directive)或 @属性(attribute)。对于像我这样记不住超过 26 个关键字的人来说,这似乎有点多,但所有的关键字、指令和属性结合得非常顺畅,就像阅读英语一样。例如,像 Golang 那样工作的 switch-case 要求用户指定每一个 case,除非你在它前面加上 #partial 指令。你会真正体会到,即使不懂这门语言,理解代码也是多么容易。

Take 10 minutes to read demo.odin, its got almost everything you need to get started. Yes, it does not have documentation (no, the overview is not a real documentation), but that is the beauty of it. You learn to code in Odin by reading other’s people code. Most of the standard library does not have comments or examples, but you can easily understand how to use it through its source code. No, it does not have macros, or comptime, or constexpr or decorators and it is better that way because every language that has them makes developers to waste 90% of their time googling for every library that uses them. Trust me, stick with Odin’s pre-defined directives and attributes. 花 10 分钟读一下 demo.odin,它几乎包含了你入门所需的一切。是的,它没有文档(不,概览不算真正的文档),但这正是它的美妙之处。你通过阅读他人的代码来学习用 Odin 编程。大多数标准库没有注释或示例,但你可以通过源代码轻松理解如何使用它们。不,它没有宏、comptime、constexpr 或装饰器,这样反而更好,因为任何拥有这些特性的语言都会让开发者浪费 90% 的时间去谷歌搜索使用它们的库。相信我,坚持使用 Odin 预定义的指令和属性就好。

Yes, it has generics, and they are more well-designed than Golang’s generics. No, it does not have a package manager, or go.mod file or anything else. The package is just a directory that you just copy to your project, and you call it relative to the path of the source code file. 是的,它有泛型,而且比 Golang 的泛型设计得更好。不,它没有包管理器、go.mod 文件或其他任何东西。包就是一个目录,你只需将其复制到你的项目中,然后相对于源代码文件的路径来调用它。

Yes, compiler is as simple as Golang’s compiler. 是的,编译器和 Golang 的编译器一样简单。

Yes, it has VSCode plugin with autocompletion. 是的,它有带自动补全功能的 VSCode 插件。

The trade offs Odin has manual memory management, which is why it does not have Closures, Composition, Goroutines, and Selectors. However, I will demonstrate how you can live without them. Secondly, it lacks libraries, which is why I’m trying to get you to learn it so you can copy your libraries from Go to Odin. Lastly, it does not have documentation or books, but if you have years of experience in Go, then Odin will feel like home. 权衡与取舍 Odin 采用手动内存管理,这就是为什么它没有闭包、组合、Goroutines 和 Selectors 的原因。不过,我会演示如何在没有它们的情况下进行开发。其次,它缺乏库,这就是我试图让你学习它的原因,这样你就可以把你的库从 Go 移植到 Odin。最后,它没有文档或书籍,但如果你有多年 Go 语言经验,那么 Odin 会让你感到宾至如归。

Living without a garbage collector Error handling: Living without a garbage collector means that you don’t have errors.New() or fmt.Errorf(). There is no error type in Odin. You only have Enums, Unions and Structs, and that is better than Go’s errors. Go’s errors suck. Golang’s errors makes you push server-side error messages to users, and you cannot do anything to stop it. That is not a good thing because users don’t need to know what is SQL or that the bank_account is nil. Furthermore, another problem is that Go’s errors do not have stack traces. 没有垃圾回收器的生活 错误处理:没有垃圾回收器意味着你没有 errors.New() 或 fmt.Errorf()。Odin 中没有错误类型。你只有枚举(Enums)、联合(Unions)和结构体(Structs),这比 Go 的错误处理更好。Go 的错误处理很糟糕。Golang 的错误处理迫使你将服务器端的错误信息推送给用户,而你对此无能为力。这不是一件好事,因为用户不需要知道什么是 SQL,也不需要知道 bank_account 是否为空。此外,另一个问题是 Go 的错误没有堆栈跟踪。