Category: Golang

  • Go by Example: Embed Directive

    The Go embed directive allows embedding of one struct type into another. The fields and methods of the embedded type become part of the outer type and can be accessed directly without a dot notation prefix. Here’s an example: In this example, the Person struct is embedded in the Developer struct, and the Person.Greet method…

  • Go by Example: Testing and Benchmarking

    In Go, testing and benchmarking of code is done through the use of the testing package. Here’s an example of how to write tests for a function: In this example, the TestAddition function tests the add function. The t *testing.T argument to the test function provides methods for testing and reporting errors. The t.Errorf method…

  • Go by Example: Command-Line Arguments

    In Go, command-line arguments are available in the os.Args slice. Here’s an example of how to read command-line arguments in Go: In this example, the os.Args slice is assigned to the args variable. The for loop iterates over the slice, printing the index and value of each argument. The first argument, os.Args[0], is the name…

  • Go by Example: Command-Line Flags

    The following example demonstrates how to use command-line flags in Go: When you run the program, you can pass in flags to modify the default values: The flag.String, flag.Int, and flag.Bool functions create new variables of the specified types, bind them to the flags with the specified names, and assign their addresses to the pointers…

  • Go by Example: Command-Line Subcommands

    The following example demonstrates how to use subcommands in Go: In this example, we use the flag.NewFlagSet function to create two different sets of flags, one for each of our two subcommands create and delete. We then use a switch statement to parse the subcommand and set the corresponding flags. Finally, we use the values…

  • Go by Example: Environment Variables

    In this example, the os package’s Setenv and Getenv functions are used to set and get an environment variable named FOO. The os.Environ function returns a slice of strings representing all environment variables, which are then printed out.

  • Go by Example: HTTP Client

    This example uses the http package’s Get function to make a GET request to http://www.google.com. The Response struct returned by Get contains the response status, headers, and body. The body is read using a bufio.Scanner and the first 5 lines are printed out. The defer statement is used to ensure that the response body is…

  • Go by Example: HTTP Server

    This example starts an HTTP server that listens on port 8080 and responds to incoming requests. The http.HandleFunc function is used to register a function that will handle incoming requests. This function writes a message to the http.ResponseWriter indicating the requested URL path. The http.ListenAndServe function is used to start the HTTP server and listen…

  • Go by Example: Context

    This example shows how to use the context package to enforce a timeout on an HTTP request. The context.WithTimeout function is used to create a context that is cancelled after the specified timeout of one second. The http.NewRequestWithContext function is used to create an HTTP request with the specified context. The request is sent using…

  • Go by Example: Spawning Processes

    In Go, you can spawn a new process by using the os/exec package. Here’s an example of how to run the ls command and retrieve its output: This program runs the ls command and retrieves its output using the StdoutPipe method of the exec.Cmd struct. The standard output of the command is read using a…