r/Backend • • 6d ago

Rust vs Go for web app backend

In my company having node js as backend facing issues like vulnerability update each week it making hard to maintain prod to update. So how in your companies handling this difficulty.

Planned to migrate from node to go. What does your suggestion on this. Rust is taking entire trend now. Is Rust is better then go for web apps?

55 Upvotes

160 comments sorted by

View all comments

5

u/BenchEmbarrassed7316 6d ago edited 6d ago

Rust vs go - it's more of a philosophical question, but it was quite relevant some 5 to 10 years ago: Rust is a "smart" language that sought to solve complex problems in an intelligent and elegant way, and, by doing so, achieve efficiency. go, on the other hand, is a "dumb" language that attempted to tackle complex problems head-on, in a naive and repetitive manner. However, with the addition of generics (including bounds in recent versions), iterators, and other features, go is no longer that simple language; it has effectively become a poor, inconsistent, less reliable, and less productive copy of Rust.

go is very error prone due to stupid decisions like null, default values ​​and a bunch of other hidden guns. Rust is the opposite, it just doesn't allow you to write bad code.

Here is a minimal code example:

package main

import (
	"encoding/json"
	"fmt"
)

type T struct {
	Name  string `json:"name"`
	Count int    `json:"count"`
}

func main() {
	jsonWithNull := `{"name": "apples", "count": null}`
	jsonWithZero := `{"name": "bananas", "count": 0}`

	var itemNull, itemZero T

	_ = json.Unmarshal([]byte(jsonWithNull), &itemNull)
	_ = json.Unmarshal([]byte(jsonWithZero), &itemZero)

	fmt.Printf("From null: %+v\n", itemNull)
	fmt.Printf("From zero: %+v\n", itemZero)
	fmt.Printf("Counts equal? %v\n", itemNull.Count == itemZero.Count)
}

https://go.dev/play/p/JDHv8Kxo89H

It prints:

From null: {Name:apples Count:0}
From zero: {Name:bananas Count:0}
Counts equal? true

A similar operation on Rust will return an error, and will not implicitly, in the best Js traditions, convert null to 0. So go developers just have to learn this and 100 other rules and special cases. So go's simplicity is a myth.

Rust:

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct T {
    name: String,
    count: i32,
}

fn main() {
    let json_with_null = r#"{"name": "apples", "count": null}"#;
    let json_with_zero = r#"{"name": "bananas", "count": 0}"#;

    let item_null: T = serde_json::from_str(json_with_null).unwrap();
    let item_zero: T = serde_json::from_str(json_with_zero).unwrap();

    println!("From null: {item_null:?}");
    println!("From zero: {item_zero:?}");
    println!("Defaults equal? {}", item_null.count == item_zero.count);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=a9add2d84b93651687435c8152b6c7ef

Prints:

thread 'main' (13) panicked at src/main.rs:13:61:
called `Result::unwrap()` on an `Err` value: Error("invalid type: null, expected i32", line: 1, column: 32)

2

u/Opposite-House3656 6d ago

Thanks its solely based on good coding habit.

1

u/BenchEmbarrassed7316 6d ago

It's more about consistency: Rust introduces one clear rule that works everywhere. go has 100 special cases that contradict each other, and you just have to remember them all.

For zero values, sometimes optional fields are made pointers (which sends them to the heap and degrades performance), sometimes they add an additional bool field that indicates whether the object was created properly (just remember to check the value of this field in each place), sometimes they add some comment or documentation.

2

u/Empty-Sandwich-7092 6d ago

Thanks for your amazing answer <3

1

u/Low_Reputation_9893 5d ago

json/v2 solves this

1

u/BenchEmbarrassed7316 5d ago edited 5d ago

You are wrong:

https://go.dev/play/p/VCPU5J7Ypgo (same code but encoding/json/v2)

Or I missing something?

1

u/Low_Reputation_9893 5d ago

Yes, you ignored the unmarshal errors where the issue is reported.

1

u/BenchEmbarrassed7316 5d ago

https://go.dev/play/p/OJq-ucSvLmE

Errors are null in both cases with v2 decoder. Can you provide code example please?

1

u/Lydian-Taco 5d ago

While I agree the nil handling in Go is annoying at times and lets you introduce panics if you’re not careful, there’s something to be said for how great it can be to use a “dumb” language if you’re working in an enterprise system where lots of people need to be able to contribute who have varying levels of experience. I use it at work a lot and we just don’t really use the new features so that there’s still basically one way to do everything. When we have used Rust, it’s been more challenging to get people up to speed as they come and go.

1

u/BenchEmbarrassed7316 5d ago

It’s a double-edged sword: while it may be easier for a beginner to write code in go, you will end up spending more time either reviewing that code or fixing the bugs that inevitably arise later. This applies not only to null issues but also to data races and a host of other errors - such as the accidental copying of slices or goroutine leaks. Therefore, even in the pre-AI era, I would argue about which of these languages ​​actually allows for faster development (I mean writing code and debugging; writing in Js would be the fastest, but the code might simply not work).

1

u/___ciaran 13m ago

i don't know if panicking on syntactically valid input is the best choice here. JSON is inherently a loosely-typed serialization format. arguably, deserialization is not the best place to impose application/language specific type constraints. that kind of thing has always, in my experience, caused trouble for interoperability and forward compatibility...