Category: Golang

  • Go by Example: Number Parsing

    The strconv package in Go provides functions for converting strings to and from various numerical types, including integers and floats. Here’s an example that demonstrates how to use the strconv package to parse numbers from strings: In this example, the strconv.Atoi function is used to parse an int from a string, and the strconv.ParseFloat function…

  • Go by Example: Random Numbers

    The math/rand package in Go provides functions for generating random numbers, making it easy to write programs that use random values. Here’s an example that demonstrates how to use the rand package to generate random numbers: In this example, the rand.Seed function is called with the current time in nanoseconds to seed the random number…

  • Go by Example: Time Formatting / Parsing

    The time package in Go provides several methods for formatting and parsing time values, making it easy to convert between human-readable string representations of times and the underlying Time type used by Go. Here’s an example that demonstrates how to use the Format and Parse methods to format a time value as a string and…

  • Go by Example: Epoch

    The Unix epoch is a specific point in time, represented as the number of seconds that have elapsed since 00:00:00 UTC on January 1, 1970. In Go, the time package provides a Time type that can be used to represent times as the number of nanoseconds elapsed since the Unix epoch. Here’s an example that…

  • Go by Example: Time

    Go has built-in support for working with dates and times through the time package. Here’s a basic example that demonstrates how to use the time package to get the current time, format it, and use it to measure elapsed time: In this example, the time.Now function is used to get the current time, which is…

  • Go by Example: XML

    Go provides built-in support for encoding and decoding XML data. XML (eXtensible Markup Language) is a widely-used data interchange format that is human-readable and easy to generate and parse. Here’s a basic example that demonstrates how to encode a Go value into XML and then decode it back into a Go value: In this example,…

  • JSON marshal example

    Here’s a simple example that demonstrates how to use the json.Marshal function in Go to encode a Go value into a JSON-encoded byte slice: In this example, a type Person is defined with two fields, Name and Age. A value of type Person is created and stored in the p variable. The json.Marshal function is…

  • Go by Example: JSON

    Go provides built-in support for encoding and decoding JSON data. JSON (JavaScript Object Notation) is a widely-used data interchange format that is human-readable and easy to generate and parse. Here’s a basic example that demonstrates how to encode a Go value into JSON and then decode it back into a Go value: In this example,…

  • Go by Example: Regular Expressions

    Go provides a package called “regexp” for working with regular expressions. Regular expressions are a pattern-matching language that can be used to match and manipulate strings. Here’s a basic example that demonstrates how to use regular expressions in Go: In this example, a variable text is defined with a string to be matched. The regexp.MustCompile…

  • Go by Example: Text Templates

    Go provides a package called “text/template” for processing text templates. Text templates are a way to embed dynamic data into a string, using placeholders that can be replaced with actual values at runtime. Here’s a basic example that demonstrates how to use text templates in Go: In this example, two variables, name and age, are…