Skip to content

Latest commit

History

85 Commits

Folders and files

NameName
Last commit message
Last commit date

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

IDExampleDescriptionLink
1Hello WorldBasic example of printing "Hello, World!" to the console.001_hello_world
2Variables & TypesDemonstrates variable declaration, initialization, and types.002_variables
3Control FlowShows usage of for loops, while loops, if-else statements, and switch statements.003_for_while_if_else_switch
4Array, Slice, Range, MapDemonstrates array and slice operations, range iteration, and map manipulation.004_array_slice_range_map
5FunctionsIllustrates basic function usage, variadic functions, closures, and recursion.005_function
6PointersShows how to use pointers to modify values, structs, slices, and maps. Includes pointer-to-pointer operations.006_pointer
7Strings & RunesExplains how to manipulate strings and runes, including encoding, slicing, and comparing.007_strings_runes
8StructsDemonstrates struct declaration, methods, embedded structs, JSON tags, and more.008_structs
9InterfacesCovers defining and implementing interfaces, type assertions, type switches, and using methods with multiple interfaces.009_interfaces
10Error HandlingShows basic error creation, custom error types, error wrapping, unwrapping, and checking for specific errors.010_error
11Goroutines & ChannelsDemonstrates using Goroutines for concurrency, WaitGroups for synchronization, channels for communication, and mutex for safe access to shared data.011_goroutine_channel
12BufferingShows how to use buffered channels, select statements, and bytes.Buffer, along with a custom buffer implementation.012_buffering
13Channel SynchronizationDemonstrates different ways of using channels for synchronization, including WaitGroups, one-way channels, and fan-out/fan-in patterns.013_channel_synchronization
14Channel DirectionsShows how to use directional channels (send-only and receive-only) in Go, with examples including ping-pong, pipelining, and buffered channels.014_channel_directions
15Channel SelectDemonstrates the use of select statement with multiple channels to handle concurrent events.015_channel_select
16TimeoutsIllustrates how to use timeouts with Goroutines and channels to control the execution flow.016_timeouts
17Channel Non-BlockingDemonstrates non-blocking operations on channels using the select statement.017_channel_non_blocking
18Basic Channel ClosingShows how to close a channel and handle its closure properly.01_basic_channel_closing
Detecting Closed ChannelExplains how to detect if a channel has been closed and how to handle it.02_detecting_closed_channel
Closing Channels in Multiple GoroutinesDemonstrates how to safely close channels when multiple Goroutines are involved.03_closing_channels_in_multiple_goroutines
Sending a Signal with a Closed ChannelShows 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 ChannelExplains what happens when you attempt to close an already closed channel and how to handle it.05_panic_when_closing_an_already_closed_channel
19Basic Example with Range Over ChannelShows 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 ChannelDemonstrates 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 RangeShows how to use buffered channels in conjunction with range to manage concurrent operations.003_buffered_channels_with_range
20Simple TimerDemonstrates how to create and use a basic timer in Go.001_simple_timer
Stop TimerShows how to stop a timer before it triggers.002_stop_timer
Reset TimerExplains how to reset a timer to its initial state.003_reset_timer
Using time.AfterDemonstrates how to use the time.After function to create timers.004_using_time_after
Timer with SelectShows how to use timers with the select statement for time-based control flow.005_timer_with_select
21Basic TickerDemonstrates how to create and use a basic ticker in Go.01_basic_ticker
Stop TickerShows how to stop a ticker before it triggers the next tick.02_stop_ticker
Ticker with SelectIllustrates how to use the select statement with a ticker for time-based control flow.03_ticker_with_select
Reset TickerExplains how to reset a ticker to its initial state.04_reset_ticker
Ticker with Limited TicksShows how to stop a ticker after a specific number of ticks.05_ticker_with_limited_ticks
22Basic Worker PoolDemonstrates how to implement a simple worker pool in Go.001_basic_worker_pool
Worker Pool with Buffered ChannelsShows how to implement a worker pool with buffered channels.002_worker_pool_with_buffered_channels
Worker Pool with Error HandlingIllustrates how to handle errors in a worker pool.003_worker_pool_with_error_handling
Dynamic Worker PoolExplains how to create a dynamic worker pool that adjusts based on workload.004_dynamic_worker_pool
Rate-Limited Worker PoolShows how to limit the rate at which jobs are processed in a worker pool.005_rate_limited_worker_pool
23Basic WaitGroupDemonstrates basic usage of `sync.WaitGroup` for synchronizing goroutines.001_basic_waitgroup
WaitGroup with Anonymous FunctionsShows how to use `sync.WaitGroup` with anonymous functions in goroutines.002_waitGroup_with_anonymous_functions
WaitGroup with Multiple WaitsIllustrates using `sync.WaitGroup` to manage multiple stages of goroutines.003_waitgroup_with_multiple_waits
WaitGroup with Error HandlingDemonstrates using `sync.WaitGroup` with error handling in workers.004_waitgroup_with_error_handling
24Basic Rate LimiterDemonstrates a basic implementation of a rate limiter.001_basic_rate_limiter
Rate Limiter with Burst CapacityShows 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
25Basic 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-SwapShows 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 OperationsShows how to use atomic load and store operations with counters.004_atomic_counter_with_load_and_store_operations
Atomic PointerDemonstrates how to atomically load and store a pointer value.005_atomic_pointer
26Basic Sorting with IntegersDemonstrates basic sorting operations with integer slices.001_basic_sorting_with_integers
Sorting StringsShows how to sort slices of strings.002_sorting_strings
Sorting by Custom SliceDemonstrates sorting a slice based on custom sorting logic.003_sorting_by_custom_slice
Sorting by Multiple FieldsShows 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 SortingShows how to sort slices in reverse order.006_reverse_sorting
Sorting with Custom Comparator FunctionDemonstrates sorting with a custom comparator function.007_sorting_with_custom_comparator_function
Sorting a Map by KeysShows how to sort a map by its keys.008_sorting_a_map_by_keys
Sorting a Map by ValuesDemonstrates sorting a map by its values.009_sorting_a_map_by_values
Concurrent Sorting with GoroutinesShows how to use goroutines to sort slices concurrently.010_concurrent_sorting_with_goroutines
27Basic Example of DeferDemonstrates the basic usage of the `defer` keyword in Go.001_basic_example_of_defer
Multiple DeferShows how multiple `defer` statements work and their order of execution.002_multiple_defer
Defer with Function CallDemonstrates using `defer` with function calls.003_defer_with_function_call
Basic Example of PanicShows a basic example of using the `panic` function in Go.004_basic_example_of_panic
Panic with RecoverDemonstrates how to use `recover` to handle panics and prevent program crashes.005_panic_with_recover
Panic with DeferShows how `defer` can be used alongside `panic` for resource cleanup.006_panic_with_defer
Chain Panic and DeferDemonstrates chaining of `panic` and `defer` statements to understand their interactions.007_chain_panic_and_defer
Defer Log Functions ExitShows how to use `defer` to log function exits and track function calls.008_defer_log_functions_exit
Closing Resources with DeferDemonstrates how to use `defer` to ensure resources like files are closed properly.009_closing_resources_with_defer
Defer with HTTPS RequestsShows how to use `defer` to handle HTTP response body cleanup after making requests.010_defer_with_https_requests
28String ManipulationsDemonstrates various string manipulation techniques in Go.001_string_manipulations
29Conditional Logic in TemplatesDemonstrates using conditional logic inside Go templates.001_conditional_logic_in_templates
Loops in TemplatesShows how to use loops to iterate over data in Go templates.002_loops_in_templates
Template FunctionsDemonstrates using built-in and custom functions in Go templates.003_template_functions
Nested TemplatesShows how to use nested templates for organizing reusable templates.004_nested_templates
Template with Data FormattingDemonstrates formatting data within Go templates, such as dates and numbers.005_template_with_data_formatting
Complex Structs and Template ActionsShows how to handle deeply nested struct fields in Go templates using the `with` action.006_complex_structs_and_template_actions
30Basic JSON Encoding and DecodingDemonstrates basic JSON encoding in Go.001_basic_encoding_json
Basic JSON DecodingShows how to decode JSON into Go structs.002_basic_decoding_json
Handling Nested JSON StructuresDemonstrates handling nested JSON structures in Go.003_handling_nested_json_structures
Working with JSON ArraysShows how to work with JSON arrays in Go.004_working_with_json_arrays
Custom JSON Field NamesDemonstrates how to use custom JSON field names in Go structs.005_custom_json_field_names
Omitting Empty FieldsShows how to omit empty fields from JSON encoding.006_omitting_empty_fields
Custom JSON Marshaling and UnmarshalingDemonstrates custom marshaling and unmarshaling of JSON data.007_custom_json_marshaling_and_unmarshaling
Decoding JSON into a MapShows 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 FieldsDemonstrates how to decode JSON while capturing unknown fields into a map.009_decoding_json_with_unknown_fields
Streaming JSON EncodingShows 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

Releases

Packages

Used by

Contributors

Languages