Avatar ·

How to properly create constructors in Go if the language has no classes and constructors?

Hello everyone! I just started learning Go after Python and Java, and I've hit a serious roadblock. Go doesn't have classes in the traditional sense and no constructors. So how do you create objects with complex initialization logic?\n\nFor example, I want to create a `User` struct with fields `Name`, `Email`, and `Age`. Ideally, I want to validate the email, ensure age is not negative, and name is not empty when creating a user. In Python I would write `__init__`, in Java - a constructor with checks. How do I do this in Go?\n\nCurrently I'm doing this:\n\n```go\ntype User struct {\n Name string\n Email string\n Age int\n}\n\nfunc main() {\n u := User{\n Name: "Alice",\n Email: "alice@example.com",\n Age: 30,\n }\n // But there are no checks here! You can create