Category: Golang

  • Go by Example: Exec’ing Processes

    The Go os/exec package provides a way to execute shell commands and retrieve the results. Here’s a simple example of executing a shell command and printing the output: This will execute the ls command with the arguments -l, -a, -h and print the resulting output.

  • Go by Example: Signals

    In Go, signals are a form of interrupt delivered to a running process. Go provides support for handling signals through the os/signal package. The following example demonstrates how to catch interrupt signals and shutdown gracefully: This example sets up a channel sigs to receive interrupt signals and a channel done to notify the main goroutine…

  • Go by Example: Exit

    In Go, you can use the os.Exit function to exit the program immediately with a specific status code. Here’s an example: The status code can be any integer value, and it’s typically used to indicate the status of the program. A status code of 0 indicates success, while non-zero status codes indicate an error. The…

  • Go by Example: String Functions

    Go provides a variety of built-in string functions that make it easy to manipulate and process strings. Here are some common string functions in Go: Here’s an example of using some of these string functions: In this example, the str variable is assigned the string value “Hello, World!”. The len function is used to print…

  • Go by Example: Recover

    The recover function is used in conjunction with the panic function to handle run-time errors in Go. The panic function is used to raise a run-time error, while recover is used to handle the error and recover from it. Here’s an example of using panic and recover: In this example, the divide function takes two…

  • Go by Example: Defer

    The defer statement in Go schedules a function call to be executed immediately before the function it is called in returns. The defer statement is often used to ensure that resources are properly cleaned up, even if the function returns early due to an error. Here’s an example of using defer: In this example, the…

  • Go by Example: Panic

    The panic function in Go is used to raise a runtime error, causing a program to exit immediately and print a stack trace. panic is typically used when a program encounters an unexpected error or situation that it cannot recover from. Here’s an example of using panic: In this example, a defer statement is used…

  • Go by Example: Sorting

    Go provides a sort package that provides functions for sorting slices of various data types, including integers, strings, and custom types. Here’s an example of using the sort package to sort a slice of integers: In this example, the sort.Ints function sorts the slice of integers in ascending order. The sorted slice is then printed…

  • Go by Example: Sorting

    Go provides a sort package that provides functions for sorting slices of various data types, including integers, strings, and custom types. Here’s an example of using the sort package to sort a slice of integers: In this example, the sort.Ints function sorts the slice of integers in ascending order. The sorted slice is then printed…

  • Go by Example: String Formatting

    Go provides several ways to format strings, including using fmt.Sprintf and fmt.Printf. fmt.Sprintf returns the formatted string, while fmt.Printf writes the formatted string to the standard output. Both functions use a similar syntax for specifying the format, using placeholders like %d for integers, %f for floating-point numbers, and %s for strings. Here’s an example that…