DEV Community

Chris
Chris

Posted on

Introduction to Go for TypeScript Developers πŸš€

If you're a TypeScript developer looking to explore Go (Golang), you might find its syntax and concepts familiar yet different. While TypeScript is dynamically typed with strong OOP support, Go is a statically typed, compiled, and concurrent programming language. This guide will help bridge the gap and make your transition smoother! πŸš€


🟠 Why Learn Go?

Go is gaining popularity because of its performance, simplicity, and scalability. Some key advantages include:

βœ… Fast execution: Compiles to native machine code, unlike TypeScript (which runs on Node.js or the browser).
βœ… Simplicity: A small but powerful standard library.
βœ… Concurrency: Built-in support for parallel execution with Goroutines.
βœ… Static typing: No runtime type errors, leading to safer code.
βœ… Efficient memory management: Automatic garbage collection.
βœ… Cross-platform support: Easily compiles for multiple architectures.


πŸ”΅ TypeScript vs. Go: Key Differences

Feature TypeScript Go
Typing Static & Dynamic Static
Compilation Transpiled to JavaScript Compiled to native binary
Object-Oriented Classes, Interfaces Structs, Interfaces (no classes)
Concurrency Event loop (async/await) Goroutines (lightweight threads)
Error Handling try/catch Multiple return values & error type
Package Manager npm/yarn go mod

🟑 Variables & Types

In TypeScript, you declare variables like this:

let age: number = 25;
const name: string = "Alice";
Enter fullscreen mode Exit fullscreen mode

In Go, you use var or the shorthand := operator:

package main
import "fmt"

func main() {
    var age int = 25  // Explicit type
    name := "Alice"   // Type inferred
    fmt.Println(age, name)
}
Enter fullscreen mode Exit fullscreen mode

Key Differences:

  • Type inference is available in both TypeScript and Go.
  • := is Go’s shorthand for declaring and initializing variables.
  • No need for let or const; var is rarely used in Go unless required for explicit typing.

🟒 Functions

TypeScript Function

function add(a: number, b: number): number {
  return a + b;
}
console.log(add(3, 5));
Enter fullscreen mode Exit fullscreen mode

Go Function

func add(a int, b int) int {
    return a + b
}

func main() {
    fmt.Println(add(3, 5))
}
Enter fullscreen mode Exit fullscreen mode

Key Differences:

  • Go declares types after the parameter name (a int, b int).
  • Go functions always return a value, and multiple return values are supported.
  • No function keyword; just func.

πŸ”΄ Objects vs Structs

TypeScript Object with Interface

interface User {
  name: string;
  age: number;
}

const user: User = {
  name: "Alice",
  age: 30,
};
console.log(user.name);
Enter fullscreen mode Exit fullscreen mode

Go Struct (Equivalent to Object)

package main
import "fmt"

type User struct {
    Name string
    Age  int
}

func main() {
    user := User{Name: "Alice", Age: 30}
    fmt.Println(user.Name)
}
Enter fullscreen mode Exit fullscreen mode

Key Differences:

  • No classes in Go; use struct instead.
  • Fields are capitalized for exported (public) access.
  • Objects don’t have methods attached like in TypeScript; instead, use methods on structs (shown below).

🟣 Methods on Structs (Go’s Alternative to Classes)

TypeScript Class

class User {
  constructor(public name: string, public age: number) {}

  greet() {
    return `Hello, my name is ${this.name}`;
  }
}

const user = new User("Alice", 30);
console.log(user.greet());
Enter fullscreen mode Exit fullscreen mode

Go Struct with Method

package main
import "fmt"

type User struct {
    Name string
    Age  int
}

// Method attached to User struct
func (u User) Greet() string {
    return "Hello, my name is " + u.Name
}

func main() {
    user := User{Name: "Alice", Age: 30}
    fmt.Println(user.Greet())
}
Enter fullscreen mode Exit fullscreen mode

Key Differences:

  • Methods in Go are defined using receiver functions, not inside a class.
  • Use (u User) to define methods for a struct.
  • No this keyword; u represents the struct instance.

πŸ”΅ Concurrency (Goroutines vs Async/Await)

TypeScript Asynchronous Function

async function fetchData() {
  const data = await fetch("https://api.example.com");
  return await data.json();
}
Enter fullscreen mode Exit fullscreen mode

Go Goroutine (Concurrent Execution)

package main
import (
    "fmt"
    "time"
)

func task() {
    time.Sleep(2 * time.Second)
    fmt.Println("Task Completed")
}

func main() {
    go task()  // Run task concurrently
    fmt.Println("Main function")
    time.Sleep(3 * time.Second) // Wait for goroutine to finish
}
Enter fullscreen mode Exit fullscreen mode

Key Differences:

  • TypeScript uses async/await (event loop-based concurrency).
  • Go uses goroutines (go func()) for lightweight threading.
  • Go's concurrency model is based on channels and goroutines, not callbacks or promises.

πŸ”₯ Final Thoughts: Should TypeScript Developers Learn Go?

βœ… YES! If you're working with backend systems, microservices, or high-performance applications, Go is an excellent language to learn.

When to Use TypeScript vs Go:

  • Use TypeScript for frontend apps (React, Vue, Angular) and lightweight backend services (Node.js, Firebase).
  • Use Go for backend services, APIs, databases, and systems requiring high performance and concurrency.

If you're used to TypeScript, Go might feel minimalistic but powerful. Once you get past the initial differences, you'll love its simplicity, speed, and scalability! πŸš€πŸ”₯


πŸ’¬ Have you tried Go? What’s your experience as a TypeScript developer learning Go? Let’s discuss in the comments!

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay