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.
Variable Declaration Methods Comparison
| Declaration Method | JavaScript | Go | Description |
|---|---|---|---|
| Variable Declaration | let x = 10; | x := 10 | Type inference, local scope |
| Constant Declaration | const x = 10; | const x = 10 | Cannot be reassigned |
| Explicit Type Declaration | let x: number = 10; (TypeScript) | var x int = 10 | Explicitly specify type |
| Zero Value Initialization | let x; (undefined) | var x int (0) | Default value initialization |
| Multiple Assignment | let a = 1, b = 2; | a, b := 1, 2 | Declare multiple variables simultaneously |
| Block Scope | { let x = 10; } | { x := 10 } | Limit variable scope |
| Function Scope | var x = 10; | var x = 10 | Hoisted to function top |
Type Inference Comparison
| Feature | JavaScript | Go | Description |
|---|---|---|---|
| Numeric Types | number (64-bit float) | int, int8, int16, int32, int64, uint, float32, float64 | Go has precise integer types |
| String Types | string | string | Both support UTF-8 |
| Boolean Types | boolean | bool | Same concept |
| Array Types | Array (dynamic) | [n]T (fixed), []T (slice) | Go distinguishes arrays and slices |
| Object Types | Object | struct, map | Go uses structs as primary data structure |
| Null Values | null, undefined | nil | Go uses nil uniformly |
| Type Checking | Runtime | Compile time | Go provides compile-time type safety |
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.
Detailed Data Types Comparison
| Data Type | JavaScript | Go | Zero Value | Description |
|---|---|---|---|---|
| Integer | number | int, int8, int16, int32, int64 | 0 | Go has precise integer sizes |
| Unsigned Integer | None | uint, uint8, uint16, uint32, uint64 | 0 | Go supports unsigned integers |
| Float | number | float32, float64 | 0.0 | Go distinguishes single/double precision |
| Complex | None | complex64, complex128 | 0+0i | Go has built-in complex number support |
| String | string | string | "" | Both support UTF-8 encoding |
| Boolean | boolean | bool | false | Same concept |
| Null Value | null, undefined | nil | nil | Go uses nil uniformly |
| Array | Array | [n]T | [0,0,0] | Go arrays have fixed size |
| Dynamic Array | Array | []T (slice) | nil | Go slices are dynamic |
| Object | Object | struct | Struct zero value | Go structs have defined structure |
| Map | Map | map[K]V | nil | Key-value storage |
| Set | Set | No built-in | - | Can be simulated with map in Go |
| Function | Function | func | nil | Function type |
| Channel | None | chan T | nil | Go concurrent communication |
| Interface | None | interface{} | nil | Go polymorphism support |
| Pointer | None | *T | nil | Go memory address reference |
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.
Variable Scope Comparison
JavaScript and Go have some important differences in variable scope.
Scope Rules Comparison
| Scope Type | JavaScript | Go | Description |
|---|---|---|---|
| Global Scope | var x = 10; | var x = 10 (package level) | Both support global variables |
| Function Scope | function() { var x = 10; } | func() { x := 10 } | Variables inside functions |
| Block Scope | { let x = 10; } | { x := 10 } | Variables within braces |
| Hoisting Behavior | var hoisted to function top | No hoisting | Go variables must be declared before use |
| Closure Support | Fully supported | Fully supported | Both support function closures |
| Variable Shadowing | Supported | Supported | Inner variables shadow outer ones |
| Loop Variables | for (let i = 0; ...) | for i := 0; ... | Loop counter scope |
Variable Lifetime Comparison
| Lifetime Feature | JavaScript | Go | Description |
|---|---|---|---|
| Garbage Collection | Automatic | Automatic | Both use garbage collection |
| Memory Management | Automatic | Automatic | Developers don't need manual management |
| Variable Destruction | At scope end | At scope end | Automatic cleanup |
| Reference Counting | Automatic | Automatic | Reference counting management |
| Memory Leaks | Closures may cause | Closures may cause | Need to watch for circular references |
Basic Data Type Comparison
Go has a comprehensive set of built-in types with specific purposes.
| JavaScript Type | Go Equivalent(s) | Notes |
|---|---|---|
number | int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, complex64, complex128 | Go has specific integer sizes |
string | string | UTF-8 encoded by default |
boolean | bool | Same concept |
null | nil | Used for pointers, interfaces, slices, maps, channels |
undefined | No direct equivalent | Often represented by nil or zero values |
object | struct, map[string]interface{} | Structs are the primary way to group data |
array | [n]T (array), []T (slice) | Arrays are fixed-size, slices are dynamic |
symbol | No direct equivalent | Not needed in Go's type system |
bigint | int64, uint64, or external packages | For arbitrary precision |
Operators and Expressions
Most operators are similar, but Go has some differences in syntax and behavior.
Practice Questions:
- Explain the difference between Go's
arrayandslicetypes, and when you would use each. - Write a Go function that takes a slice of integers and returns the sum and average, demonstrating multiple return values.
- How does Go's error handling differ from JavaScript's exception handling? Provide examples of both approaches.
- Create a Go struct to represent a
Personwith 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