The big 3 cool languages for making binary executables. Learning in the wrong way with ChatGPT.
Why these 3?
The goal is to create binary executables for all platforms. So, I picked these three:
- Go: Because everyone uses it.
- Rust: Because the cool kids tell you they use it.
- Nim: Because I am a Python developer.
Honorable mention:
- Typescript: The only way to create an executables with Typescript is with
deno compile
. Check out mydeno compile
writeup to see why I will not touch it again. - Python: Not worth exploring it here.
- Zig: Everyone says it is the new cool thing, but let’s wait till 2030.
Disclaimer: I will be using ChatGPT to tutor me through this process.
Nim
ChatGPT spits out this code:
import os
proc printHello(input: string) =
echo "Hello, " & input
proc main() =
var input: string
echo "Enter your name:"
readLine(input)
printHello(input)
when isMainModule:
main()
So, ChatGPT starts with bad code.
- Unnecessary import of the
os
library. - Error related to
readline
.proc readLine(f: File): string required type for f: File but expression 'input' is of type: string
- The variable input is being declared then subsequently that variable is passed to the
readLine
function??
- Needs better type declaration and assignments.
Although I like they are are using isMainModule
here, which I believe is the same as if __name__ == '__main__'
of Python.
The acceptable code:
proc printHello(input: string) =
echo "Hello, " & input
proc main() =
echo "Enter your name:"
const input: str = readLine(stdin)
printHello(input)
when isMainModule:
main()
Running the script:
nim c -r hello.nim
For release build:
nim c -d:release hello.nim
Rust
Moving on to Rust.
use std::io;
fn print_hello(input: &str) {
println!("Hello, {}!", input);
}
fn main() {
println!("Enter your name:");
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read input");
let input = input.trim();
print_hello(input);
}
I have no idea how to even attempt to read Rust code. But I will try to learn as I go. Compared to our previous script:
- We are using a
mut
for mutable declaration - We have error catch statement via
expect
- We are running a
trim
operation which strips whitespaces of input string.
Initiate a Rust project with:
cargo new hello
Running the script:
cargo run
For release build:
cargo build --release
Go
Time for Go.
package main
import (
"bufio"
"fmt"
"os"
)
func printHello(input string) {
fmt.Printf("Hello, %s!\n", input)
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Enter your name:")
input, _ := reader.ReadString('\n')
input = input[:len(input)-1]
printHello(input)
}
Why the heck are we importing bufio
? I asked ChatGPT that and it said sorry and gave me this:
package main
import (
"fmt"
"os"
)
func printHello(input string) {
fmt.Printf("Hello, %s!\n", input)
}
func main() {
var input string
fmt.Println("Enter your name:")
_, err := fmt.Scanln(&input)
if err != nil {
fmt.Println("Error reading input:", err)
os.Exit(1)
}
printHello(input)
}
This looks more okay to me.
So, Running this:
go run .\hello.go
go build -ldflags "-s -w" .\hello.go
TLDR
Rank | Language | Description | size |
---|---|---|---|
🦀 | Rust | Good code, but no idea what I am reading. | 167 KB |
👑 | Nim | Not popular enough to have good AI generated code. | 176 KB |
🐀 | Go | Bigger executable file, and moderately bad AI generated code. | 1,420 KB |
If you are going to learn a language, pick a popular one with lots of answers on the web.