Skip to content

Rapter1990/go_sample_examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Sample Examples

Main Information

📖 Information

  • This project showcases sample Go applications demonstrating various functionalities, including data processing, web server management, and concurrent programming techniques.
  • It is designed to help developers understand Go's syntax, features, and best practices through practical examples.
  • The repository includes:
    • Examples of basic Go data structures, functions, and interfaces.
    • Web server setup using Go's standard library for HTTP handling.
    • Concurrency examples using goroutines, channels, and the Go scheduler.
    • File handling operations like reading from and writing to files.
    • Working with arrays, slices, and maps to demonstrate Go's powerful collection types.
    • JSON encoding and decoding examples, including working with custom data structures.
    • Error handling following Go idioms to demonstrate best practices for robust programs.
    • Basic data parsing, string manipulation, arithmetic operations, and JSON handling in Go.

Explore Go Sample Examples

Examples Summary

ID Example Description Link
1 Hello World Basic example of printing "Hello, World!" to the console. 001_hello_world
2 Variables & Types Demonstrates variable declaration, initialization, and types. 002_variables
3 Control Flow Shows usage of for loops, while loops, if-else statements, and switch statements. 003_for_while_if_else_switch
4 Array, Slice, Range, Map Demonstrates array and slice operations, range iteration, and map manipulation. 004_array_slice_range_map
5 Functions Illustrates basic function usage, variadic functions, closures, and recursion. 005_function
6 Pointers Shows how to use pointers to modify values, structs, slices, and maps. Includes pointer-to-pointer operations. 006_pointer
7 Strings & Runes Explains how to manipulate strings and runes, including encoding, slicing, and comparing. 007_strings_runes
8 Structs Demonstrates struct declaration, methods, embedded structs, JSON tags, and more. 008_structs
9 Interfaces Covers defining and implementing interfaces, type assertions, type switches, and using methods with multiple interfaces. 009_interfaces
10 Error Handling Shows basic error creation, custom error types, error wrapping, unwrapping, and checking for specific errors. 010_error
11 Goroutines & Channels Demonstrates using Goroutines for concurrency, WaitGroups for synchronization, channels for communication, and mutex for safe access to shared data. 011_goroutine_channel
12 Buffering Shows how to use buffered channels, select statements, and bytes.Buffer, along with a custom buffer implementation. 012_buffering
13 Channel Synchronization Demonstrates different ways of using channels for synchronization, including WaitGroups, one-way channels, and fan-out/fan-in patterns. 013_channel_synchronization
14 Channel Directions Shows how to use directional channels (send-only and receive-only) in Go, with examples including ping-pong, pipelining, and buffered channels. 014_channel_directions
15 Channel Select Demonstrates the use of select statement with multiple channels to handle concurrent events. 015_channel_select
16 Timeouts Illustrates how to use timeouts with Goroutines and channels to control the execution flow. 016_timeouts
17 Channel Non-Blocking Demonstrates non-blocking operations on channels using the select statement. 017_channel_non_blocking
18 Basic Channel Closing Shows how to close a channel and handle its closure properly. 01_basic_channel_closing
Detecting Closed Channel Explains how to detect if a channel has been closed and how to handle it. 02_detecting_closed_channel
Closing Channels in Multiple Goroutines Demonstrates how to safely close channels when multiple Goroutines are involved. 03_closing_channels_in_multiple_goroutines
Sending a Signal with a Closed Channel Shows the behavior of sending signals through a closed channel and how to manage it. 04_sending_a_signal_with_a_closed_channel
Panic When Closing an Already Closed Channel Explains what happens when you attempt to close an already closed channel and how to handle it. 05_panic_when_closing_an_already_closed_channel
19 Basic Example with Range Over Channel Shows how to use the range keyword to iterate over values received from a channel. 001_basic_example_range_over_channel
Multiple Goroutines Sending to a Channel Demonstrates how multiple Goroutines can send data to a single channel and how to handle it. 002_multiple_goroutines_sending_to_a_channel
Buffered Channels with Range Shows how to use buffered channels in conjunction with range to manage concurrent operations. 003_buffered_channels_with_range
20 Simple Timer Demonstrates how to create and use a basic timer in Go. 001_simple_timer
Stop Timer Shows how to stop a timer before it triggers. 002_stop_timer
Reset Timer Explains how to reset a timer to its initial state. 003_reset_timer
Using time.After Demonstrates how to use the time.After function to create timers. 004_using_time_after
Timer with Select Shows how to use timers with the select statement for time-based control flow. 005_timer_with_select
21 Basic Ticker Demonstrates how to create and use a basic ticker in Go. 01_basic_ticker
Stop Ticker Shows how to stop a ticker before it triggers the next tick. 02_stop_ticker
Ticker with Select Illustrates how to use the select statement with a ticker for time-based control flow. 03_ticker_with_select
Reset Ticker Explains how to reset a ticker to its initial state. 04_reset_ticker
Ticker with Limited Ticks Shows how to stop a ticker after a specific number of ticks. 05_ticker_with_limited_ticks
22 Basic Worker Pool Demonstrates how to implement a simple worker pool in Go. 001_basic_worker_pool
Worker Pool with Buffered Channels Shows how to implement a worker pool with buffered channels. 002_worker_pool_with_buffered_channels
Worker Pool with Error Handling Illustrates how to handle errors in a worker pool. 003_worker_pool_with_error_handling
Dynamic Worker Pool Explains how to create a dynamic worker pool that adjusts based on workload. 004_dynamic_worker_pool
Rate-Limited Worker Pool Shows how to limit the rate at which jobs are processed in a worker pool. 005_rate_limited_worker_pool
23 Basic WaitGroup Demonstrates basic usage of `sync.WaitGroup` for synchronizing goroutines. 001_basic_waitgroup
WaitGroup with Anonymous Functions Shows how to use `sync.WaitGroup` with anonymous functions in goroutines. 002_waitGroup_with_anonymous_functions
WaitGroup with Multiple Waits Illustrates using `sync.WaitGroup` to manage multiple stages of goroutines. 003_waitgroup_with_multiple_waits
WaitGroup with Error Handling Demonstrates using `sync.WaitGroup` with error handling in workers. 004_waitgroup_with_error_handling
24 Basic Rate Limiter Demonstrates a basic implementation of a rate limiter. 001_basic_rate_limiter
Rate Limiter with Burst Capacity Shows how to implement rate limiting with burst capacity. 002_rate_limiter_burst_capacity
Custom Rate Limiter Using `time.After` Demonstrates custom rate limiting using `time.After` for more flexibility. 003_custom_rate_limiter_using_time_after
Rate Limiter with `context.Context` Shows how to implement rate limiting using `context.Context` for request cancellation and control. 004_rate_limiter_with_context
Rate Limiter with `time.NewTicker` Demonstrates rate limiting using `time.NewTicker` for better control over the ticker's lifecycle. 005_rate_limiter_with_time_newticker
25 Basic Atomic Counter Using `sync/atomic` Demonstrates a simple atomic counter using the `sync/atomic` package. 001_basic_atomic_counter_using_sync_atomic
Atomic Counter with Decrement and Compare-And-Swap Shows an atomic counter with decrement and compare-and-swap operations. 002_atomic_counter_with_decrement_and_compare_and_swap
Atomic Flag Using `sync/atomic` Demonstrates the use of an atomic flag for state management with `sync/atomic`. 003_atomic_flag_using_sync_atomic
Atomic Counter with Load and Store Operations Shows how to use atomic load and store operations with counters. 004_atomic_counter_with_load_and_store_operations
Atomic Pointer Demonstrates how to atomically load and store a pointer value. 005_atomic_pointer
26 Basic Sorting with Integers Demonstrates basic sorting operations with integer slices. 001_basic_sorting_with_integers
Sorting Strings Shows how to sort slices of strings. 002_sorting_strings
Sorting by Custom Slice Demonstrates sorting a slice based on custom sorting logic. 003_sorting_by_custom_slice
Sorting by Multiple Fields Shows how to sort slices based on multiple fields. 004_sorting_by_multiple_fields
Sorting a Custom Type Using `sort.Interface` Demonstrates sorting a custom type by implementing the `sort.Interface`. 005_sorting_a_custom_type_using_sort_interface
Reverse Sorting Shows how to sort slices in reverse order. 006_reverse_sorting
Sorting with Custom Comparator Function Demonstrates sorting with a custom comparator function. 007_sorting_with_custom_comparator_function
Sorting a Map by Keys Shows how to sort a map by its keys. 008_sorting_a_map_by_keys
Sorting a Map by Values Demonstrates sorting a map by its values. 009_sorting_a_map_by_values
Concurrent Sorting with Goroutines Shows how to use goroutines to sort slices concurrently. 010_concurrent_sorting_with_goroutines
27 Basic Example of Defer Demonstrates the basic usage of the `defer` keyword in Go. 001_basic_example_of_defer
Multiple Defer Shows how multiple `defer` statements work and their order of execution. 002_multiple_defer
Defer with Function Call Demonstrates using `defer` with function calls. 003_defer_with_function_call
Basic Example of Panic Shows a basic example of using the `panic` function in Go. 004_basic_example_of_panic
Panic with Recover Demonstrates how to use `recover` to handle panics and prevent program crashes. 005_panic_with_recover
Panic with Defer Shows how `defer` can be used alongside `panic` for resource cleanup. 006_panic_with_defer
Chain Panic and Defer Demonstrates chaining of `panic` and `defer` statements to understand their interactions. 007_chain_panic_and_defer
Defer Log Functions Exit Shows how to use `defer` to log function exits and track function calls. 008_defer_log_functions_exit
Closing Resources with Defer Demonstrates how to use `defer` to ensure resources like files are closed properly. 009_closing_resources_with_defer
Defer with HTTPS Requests Shows how to use `defer` to handle HTTP response body cleanup after making requests. 010_defer_with_https_requests
28 String Manipulations Demonstrates various string manipulation techniques in Go. 001_string_manipulations
29 Conditional Logic in Templates Demonstrates using conditional logic inside Go templates. 001_conditional_logic_in_templates
Loops in Templates Shows how to use loops to iterate over data in Go templates. 002_loops_in_templates
Template Functions Demonstrates using built-in and custom functions in Go templates. 003_template_functions
Nested Templates Shows how to use nested templates for organizing reusable templates. 004_nested_templates
Template with Data Formatting Demonstrates formatting data within Go templates, such as dates and numbers. 005_template_with_data_formatting
Complex Structs and Template Actions Shows how to handle deeply nested struct fields in Go templates using the `with` action. 006_complex_structs_and_template_actions
30 Basic JSON Encoding and Decoding Demonstrates basic JSON encoding in Go. 001_basic_encoding_json
Basic JSON Decoding Shows how to decode JSON into Go structs. 002_basic_decoding_json
Handling Nested JSON Structures Demonstrates handling nested JSON structures in Go. 003_handling_nested_json_structures
Working with JSON Arrays Shows how to work with JSON arrays in Go. 004_working_with_json_arrays
Custom JSON Field Names Demonstrates how to use custom JSON field names in Go structs. 005_custom_json_field_names
Omitting Empty Fields Shows how to omit empty fields from JSON encoding. 006_omitting_empty_fields
Custom JSON Marshaling and Unmarshaling Demonstrates custom marshaling and unmarshaling of JSON data. 007_custom_json_marshaling_and_unmarshaling
Decoding JSON into a Map Shows how to decode JSON into a map with string keys and interface values. 008_decoding_json_into_a_map_string_interface
Decoding JSON with Unknown Fields Demonstrates how to decode JSON while capturing unknown fields into a map. 009_decoding_json_with_unknown_fields
Streaming JSON Encoding Shows how to use streaming JSON encoding with indentation. 010_streaming_json_encoding

Technologies


  • Go 1.23.0

Prerequisites

Ensure Go is installed on your system

    https://golang.org/dl/

Contributors