langShiftlangShift

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 MethodJavaScriptGoDescription
Variable Declarationlet x = 10;x := 10Type inference, local scope
Constant Declarationconst x = 10;const x = 10Cannot be reassigned
Explicit Type Declarationlet x: number = 10; (TypeScript)var x int = 10Explicitly specify type
Zero Value Initializationlet x; (undefined)var x int (0)Default value initialization
Multiple Assignmentlet a = 1, b = 2;a, b := 1, 2Declare multiple variables simultaneously
Block Scope{ let x = 10; }{ x := 10 }Limit variable scope
Function Scopevar x = 10;var x = 10Hoisted to function top

Type Inference Comparison

FeatureJavaScriptGoDescription
Numeric Typesnumber (64-bit float)int, int8, int16, int32, int64, uint, float32, float64Go has precise integer types
String TypesstringstringBoth support UTF-8
Boolean TypesbooleanboolSame concept
Array TypesArray (dynamic)[n]T (fixed), []T (slice)Go distinguishes arrays and slices
Object TypesObjectstruct, mapGo uses structs as primary data structure
Null Valuesnull, undefinednilGo uses nil uniformly
Type CheckingRuntimeCompile timeGo provides compile-time type safety
Loading editor...

Control Flow Statements Comparison

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

If/Else Statements

Loading editor...

For Loops

Loading editor...

Function Definition and Calling

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

Loading editor...

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 TypeJavaScriptGoZero ValueDescription
Integernumberint, int8, int16, int32, int640Go has precise integer sizes
Unsigned IntegerNoneuint, uint8, uint16, uint32, uint640Go supports unsigned integers
Floatnumberfloat32, float640.0Go distinguishes single/double precision
ComplexNonecomplex64, complex1280+0iGo has built-in complex number support
Stringstringstring""Both support UTF-8 encoding
BooleanbooleanboolfalseSame concept
Null Valuenull, undefinednilnilGo uses nil uniformly
ArrayArray[n]T[0,0,0]Go arrays have fixed size
Dynamic ArrayArray[]T (slice)nilGo slices are dynamic
ObjectObjectstructStruct zero valueGo structs have defined structure
MapMapmap[K]VnilKey-value storage
SetSetNo built-in-Can be simulated with map in Go
FunctionFunctionfuncnilFunction type
ChannelNonechan TnilGo concurrent communication
InterfaceNoneinterface{}nilGo polymorphism support
PointerNone*TnilGo memory address reference
Loading editor...

Scope and Lifetime

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

Loading editor...

Error Handling

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

Loading editor...

Variable Scope Comparison

JavaScript and Go have some important differences in variable scope.

Scope Rules Comparison

Scope TypeJavaScriptGoDescription
Global Scopevar x = 10;var x = 10 (package level)Both support global variables
Function Scopefunction() { var x = 10; }func() { x := 10 }Variables inside functions
Block Scope{ let x = 10; }{ x := 10 }Variables within braces
Hoisting Behaviorvar hoisted to function topNo hoistingGo variables must be declared before use
Closure SupportFully supportedFully supportedBoth support function closures
Variable ShadowingSupportedSupportedInner variables shadow outer ones
Loop Variablesfor (let i = 0; ...)for i := 0; ...Loop counter scope

Variable Lifetime Comparison

Lifetime FeatureJavaScriptGoDescription
Garbage CollectionAutomaticAutomaticBoth use garbage collection
Memory ManagementAutomaticAutomaticDevelopers don't need manual management
Variable DestructionAt scope endAt scope endAutomatic cleanup
Reference CountingAutomaticAutomaticReference counting management
Memory LeaksClosures may causeClosures may causeNeed to watch for circular references

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.

Loading editor...

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