Category: Golang

  • Go by Example: Stateful Goroutines

    A stateful Goroutine is a Goroutine that retains its state across multiple function invocations, allowing it to persist data between function calls. In Go, stateful Goroutines are implemented by passing data to a Goroutine as an argument and then accessing that data within the Goroutine. Here’s an example of using stateful Goroutines in Go: In…

  • Go by Example: Temporary Files and Directories

    The Go standard library provides the os and ioutil packages for working with temporary files and directories. Here’s an example that demonstrates how to use these packages to work with temporary files and directories: In this example, we use the ioutil.TempDir function to create a temporary directory, and the ioutil.TempFile function to create a temporary…

  • Go by Example: Directories

    The Go standard library provides the os package for working with the file system, including directories. Here’s an example that demonstrates how to use the os package to work with directories: In this example, we use the os.Mkdir function to create a new directory named “new_directory”. The second argument to os.Mkdir is the permission mode…

  • Go by Example: File Paths

    File paths in Go are represented by the path package in the standard library. The path package provides functions for working with file paths in a platform-independent way. Here’s an example that demonstrates some of the basic operations you can perform with file paths in Go: In this example, we first define a file path…

  • Go by Example: Line Filters

    A line filter is a common type of program that reads input from stdin, processes it, and then outputs the result to stdout. Here’s an example in Go that implements a line filter that converts all input to uppercase: In this example, we use the bufio.NewScanner function to create a new bufio.Scanner that reads from…

  • Go by Example: Writing Files

    Go provides the os package that makes it easy to write files. Here’s an example of how you can write to a file in Go: In this example, we first use the os.Create function to create a new file named “test.txt” for writing. The defer file.Close statement is used to ensure that the file is…

  • Go by Example: Reading Files

    Go provides the os and bufio packages that make it easy to read files. Here’s an example of how you can read a file line by line in Go: In this example, we first use the os.Open function to open the file “test.txt”. The defer file.Close statement is used to ensure that the file is…

  • Go by Example: Base64 Encoding

    Go provides a encoding/base64 package that implements base64 encoding and decoding. Here’s an example of how you can use the base64 package to encode and decode data: In this example, the base64.StdEncoding.EncodeToString function is used to encode a string to base64. The input to this function is a byte slice, so we first convert the…

  • Go by Example: SHA256 Hashes

    Go provides a hash package that implements various hash algorithms, including SHA256. Here’s an example of how you can use the sha256 package to compute the SHA256 hash of a string: In this example, the sha256.Sum256 function is used to compute the SHA256 hash of a string. The input to this function is a byte…

  • Go by Example: URL Parsing

    The url package in Go provides functions for parsing and manipulating URLs. Here’s an example that demonstrates how to use the url package to parse URLs: In this example, the url.Parse function is used to parse a URL string. If the parsing fails, an error is returned. Once the URL is parsed, you can access…