▲ 27 r/golang

Use Values for Null Safety

I'm trying to understand the idiomatic Go approach for expressing non-nullability.

As I understand it:

  • A struct value can never be nil.
  • A pointer (*T) can be nil, so it's commonly used when the absence of a value is meaningful.

For example:

type User struct {
    Name string
}

// User can never be nil.
func ProcessValue(user User) {
    // ...
}

// User may be nil.
func ProcessPointer(user *User) {
    // ...
}

The problem is that using a value has two different semantics:

  1. It guarantees the argument is non-nil.
  2. It copies the entire struct when passed to a function or assigned.

For small structs this isn't a concern, but for very large structs the copy can be expensive. In those cases I'd prefer to pass a pointer, but then I've lost the compile-time guarantee that the value isn't nil.

For example:

type LargeStruct struct {
    Data [1024 * 1024]byte
}

func Process(s LargeStruct) {
    // Copies the entire struct.
}

Is there an idiomatic way in Go to express "this must never be nil" while still passing a pointer?

Or is the accepted Go approach simply to use *T and rely on documentation and runtime checks when non-nil is required?

reddit.com
u/Sad_Importance_1585 — 19 hours ago
▲ 22 r/Backend

Migration to Java

Hi Guys,

Did you work in a company that decided to migrate its codebase from another language (node.js, python, C#, PHP) to Java?

What were the considerations? Did the effort pay off?

reddit.com
u/Sad_Importance_1585 — 1 day ago
▲ 0 r/Kotlin+1 crossposts

Kotlin Future in Backend Development

I see that new startups barely pick Java/Kotlin as backend language. They usually pick node.js/python/Go. We usually see Java in old companies, and these companies sometimes choose to write new stuff in Kotlin.

The old companies will somewhen get closed. If the new generations of companies do not pick Java/Kotlin, then the conclusion is that these languages will become obsolete (at least for backend) in the near future.

What do you think?

reddit.com
u/Sad_Importance_1585 — 2 days ago
▲ 79 r/microservices+1 crossposts

How to fetch data "owned" to another microservice?

Hi Guys,

Suppose I have two microservices - A and B. Each one of them owns a database. When I say "own", I mean that it is the only microservice that writes data to this microservice. Now, we all encounter situations that microservice A wants to read some specific data in database B.

There are two options:

1 - microservice A sends an HTTP call to microservice B, which reads data from microservice B and returns it back to microservice A.

2 - microservice A reads database B directly

Option 1 is considered more clean and most architecture books advocate for it. There is only one microservice that interacts with a database. On the other hand, the operational complexity is clear. The data path is longer and if (at certain point of time) microservice A wants to extend the data that it fetches from microservice A, we will have to change the code in both microservices.

I want to ask if you would consider using option 2 in order to enhance code simplicity.

u/Sad_Importance_1585 — 4 days ago