langShift

Syntax Comparison Basics

This module explores the fundamental syntax differences between JavaScript and Go, covering variable declarations, control flow, functions, data types, and other core language constructs.

Variable Declaration and Type System

JavaScript uses dynamic typing where variable types are determined at runtime, while Go uses static typing with type inference capabilities.

正在加载...

Control Flow Statements Comparison

Both languages support similar control flow structures, but with different syntax and conventions.

If/Else Statements

正在加载...

For Loops

正在加载...

Function Definition and Calling

Go functions require explicit return types and have different syntax compared to JavaScript functions.

正在加载...

Data Types and Structures

Go has a rich set of built-in types and structures that differ significantly from JavaScript.

正在加载...

Scope and Lifetime

Both languages have block scope, but Go's scoping rules are more explicit and consistent.

正在加载...

Error Handling

JavaScript uses try-catch for exception handling, while Go uses explicit error return values.

正在加载...

Basic Data Type Comparison

Go has a comprehensive set of built-in types with specific purposes.

JavaScript TypeGo Equivalent(s)Notes
numberint, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128Go has specific integer sizes
stringstringUTF-8 encoded by default
booleanboolSame concept
nullnilUsed for pointers, interfaces, slices, maps, channels
undefinedNo direct equivalentOften represented by nil or zero values
objectstruct, map[string]interface{}Structs are the primary way to group data
array[n]T (array), []T (slice)Arrays are fixed-size, slices are dynamic
symbolNo direct equivalentNot needed in Go's type system
bigintint64, uint64, or external packagesFor arbitrary precision

Operators and Expressions

Most operators are similar, but Go has some differences in syntax and behavior.

正在加载...

Practice Questions:

  1. Explain the difference between Go's array and slice types, and when you would use each.
  2. Write a Go function that takes a slice of integers and returns the sum and average, demonstrating multiple return values.
  3. How does Go's error handling differ from JavaScript's exception handling? Provide examples of both approaches.
  4. Create a Go struct to represent a Person with fields for name, age, and email, then write a function to validate the person data.

Project Idea:

  • Build a simple calculator program in Go that can perform basic arithmetic operations, demonstrating function usage, error handling, and user input processing. Compare this with a JavaScript equivalent.

Next Steps:

  • Learn about Go's package system and module management
  • Understand Go's type system and interfaces
  • Explore Go's powerful concurrency features with goroutines and channels