montana

Since
13 Snippets
  • Detecting symbolic link in Go

    package main
    
    import (
    	"fmt"
    	"os"
    	"path/filepath"
    )
    
    func IsSymlink(path string) (bool, error) {
    	var isSymlink bool
    	stat, err := os.Lstat(path)
    
    	if err != nil {
    		return isSymlink, err
    	}
    
    	isSymlink = (stat.Mode() & os.ModeSymlink) == os.ModeSymlink
    
    	return isSymlink, nil
    }
    
    func main() {
    	// create temporary file
    	tmpFile, err := os.CreateTemp("", "tmpfile")
    
    	if err != nil {
    		panic(err)
    	}
    
    	defer os.Remove(tmpFile.Name())
    
    	fmt.Println("Temp file name:", tmpFile.Name())
    
    	symlinkPath := filepath.Join(os.TempDir(), "tmpsymlink")
    
    	// create symlink
    	err = os.Symlink(tmpFile.Name(), symlinkPath)
    
    	if err != nil {
    		panic(err)
    	}
    
    	defer os.Remove(symlinkPath)
    
    	// check if file is symlink
    	isSymlink, err := IsSymlink(symlinkPath)
    
    	if err != nil {
    		panic(err)
    	}
    
    	fmt.Printf("'%s' is symlink: %v", symlinkPath, isSymlink)
    
    }
    
    // $ go run main.go 
    // Temp file name: /tmp/tmpfile1471080551
    // '/tmp/tmpsymlink' is symlink: true

    An example of determining whether a file is a symlink.

  • Parse URL in Go

    package main
    
    import (
    	"fmt"
    	"net"
    	"net/url"
    )
    
    func main() {
    	s := "mysql://user:pass@host.com:3306/path?key=value#fragment"
    
    	u, err := url.Parse(s)
    
    	if err != nil {
    		panic(err)
    	}
    
    	fmt.Println("Scheme: " + u.Scheme)
    
    	fmt.Println("Username: " + u.User.Username())
    	p, _ := u.User.Password()
    	fmt.Println("Password: " + p)
    
    	fmt.Println("Host with port: " + u.Host)
    
    	host, port, err := net.SplitHostPort(u.Host)
    
    	if err != nil {
    		panic(err)
    	}
    
    	fmt.Println("Host: " + host)
    	fmt.Println("Port: " + port)
    
    	fmt.Println("Path: " + u.Path)
    	fmt.Println("Fragment: " + u.Fragment)
    
    	fmt.Println("Raw query: " + u.RawQuery)
    
    	m, err := url.ParseQuery(u.RawQuery)
    
    	if err != nil {
    		panic(err)
    	}
    
    	fmt.Println("Value: " + m["key"][0])
    }
    
    // $ go run main.go 
    // Scheme: mysql
    // Username: user
    // Password: pass
    // Host with port: host.com:3306
    // Host: host.com
    // Port: 3306
    // Path: /path
    // Fragment: fragment
    // Raw query: key=value
    // Value: value

    Here is an example of how to parse URL, which includes a scheme, authentication info, host, port, path, query params, and query fragment.

  • Goroutines in Go

    package main
    
    import (
    	"fmt"
    	"time"
    )
    
    func print(from string) {
    	for i := 0; i < 3; i++ {
    		fmt.Println(from, ":", i)
    	}
    }
    
    func main() {
    	// Synchronous function call
    	print("synchronous call")
    
    	// Asynchronous function call
    	// To run this function in a goroutine, use go f(s)
    	// This new goroutine will execute concurrently with the calling main goroutine
    	go print("asynchronous call")
    
    	// Goroutine can also be execute as an anonymous function
    	go func(msg string) {
    		fmt.Println(msg)
    	}("asynchronous anonymous call")
    
    	// Two previous function calls are now running asynchronously in separate goroutines
    	// Wait for them to finish
    	time.Sleep(time.Second)
    	fmt.Println("done")
    }
    
    // $ go run main.go
    // synchronous call : 0
    // synchronous call : 1
    // synchronous call : 2
    // asynchronous anonymous call
    // asynchronous call : 0
    // asynchronous call : 1
    // asynchronous call : 2
    // done

    Here is an example of goroutines in Golang.

  • Buffered and unbuffered channels in Go

    package main
    
    import "fmt"
    
    func main() {
        // Create unbuffered channel
    	unbufferedChannel := make(chan string)
    
    	go func() {
            // Send a value into a channel using the channel<- syntax
    		unbufferedChannel <- "unbuffered channel"
    	}()
    
        // The <-channel syntax receives a value from the channel
    	fmt.Println(<-unbufferedChannel)
    
        // Create buffered channel with size 2
    	bufferedChannel := make(chan string, 2)
    
        // Send a value to a buffered channel. The operation in non blocking
    	bufferedChannel <- "buffered"
    	bufferedChannel <- "channel"
    
    	fmt.Println(<-bufferedChannel)
    	fmt.Println(<-bufferedChannel)
    }
    
    // go run main.go 
    // unbuffered channel
    // buffered
    // channel

    Channels serve as pipes for communication between concurrent goroutines. They allow one goroutine to send values and another goroutine to receive those values. By default, sending and receiving operations block until both the sender and receiver are ready. This feature enabled us to wait for the "unbuffered channel" message at the end of our program without needing any additional synchronization mechanisms. Buffered channels can hold a limited number of values without requiring an immediate receiver.

  • Detect OS in Go

    package main
    
    import (
        "fmt"
        "runtime"
    )
    
    func main() {
        // The runtime.GOOS constant can be used to detect the OS at runtime,
        // since this constant is only set at runtime.
        os := runtime.GOOS
    
        switch os {
        case "windows":
            fmt.Println("Windows")
        case "darwin":
            fmt.Println("MacOS")
        case "linux":
            fmt.Println("Linux")
        default:
            fmt.Printf("%s.\n", os)
        }
    
        // The runtime.GOARCH constant can be used to determine the target architecture of a running program.
        fmt.Println(runtime.GOARCH)
    }
    
    // Output:
    // Linux
    // amd64

    GOOS constant to determine the operating system your Go program is running on. Here's an example of how to check the operating system in Go.

  • Create temporary file or directory in Go

    package main
    
    import (
        "fmt"
        "os"
        "path/filepath"
    )
    
    func checkErr(err error) {
        if err != nil {
            panic(err)
        }
    }
    
    func main() {
        // The simplest way to create a temporary file is to call os.CreateTemp.
        // It will create and open the file for reading and writing.
        // We used "" as the first argument, so os.CreateTemp will create a file in the default directory.
        tmpFile, err := os.CreateTemp("", "tmpfile")
        checkErr(err)
        defer os.Remove(tmpFile.Name())
    
        fmt.Println("Temp file name:", tmpFile.Name())
    
        // Write some data to the temporary file
        _, err = tmpFile.Write([]byte{1, 2, 3, 4, 5})
        checkErr(err)
    
        // If we intend to write a lot of temporary files, we may prefer to create a temporary directory.
        // The arguments to os.MkdirTemp are the same as for os.CreateTemp, but it returns the directory name rather than the opened file.
        dName, err := os.MkdirTemp("", "tmpdir")
        checkErr(err)
        defer os.RemoveAll(dName)
        
        fmt.Println("Temp directory name:", dName)
    
        fName := filepath.Join(dName, "testFile")
        err = os.WriteFile(fName, []byte{1, 2, 3, 4, 5}, 0666)
        checkErr(err)
        defer os.Remove(fName)
    }
    
    // Output:
    // Temp file name: /tmp/tmpfile3400905374
    // Temp directory name: /tmp/tmpdir2812568099

    During program execution, we often want to create data that is not needed after the program exits. Temporary files and directories are useful for this purpose because they do not pollute the file system over time.

  • Writing files in Go

    package main
    
    import (
        "bufio"
        "fmt"
        "os"
    )
    
    func checkErr(err error) {
        if err != nil {
            panic(err)
        }
    }
    
    func main() {
        content := "Some content"
        // write a string (or just bytes) into a file
        err := os.WriteFile("/tmp/dfile1", []byte(content), 0644)
        checkErr(err)
    
        // open a file for writing
        file, err := os.Create("/tmp/dfile2")
        checkErr(err)
        // defer a Close immediately after opening a file
        defer file.Close()
    
        bytesCount, err := file.Write([]byte(content))
        checkErr(err)
        fmt.Printf("wrote %d bytes\n", bytesCount)
    
        bytesCount, err = file.WriteString("Some other content\n")
        checkErr(err)
        fmt.Printf("wrote %d bytes\n", bytesCount)
        // flush writes to a stable storage (file system for example)
        file.Sync()
    
        // bufio provides buffered writers
        writer := bufio.NewWriter(file)
        bytesCount, err = writer.WriteString("Some othe buffered content\n")
        checkErr(err)
        fmt.Printf("wrote %d bytes\n", bytesCount)
        // ensure all buffered operations have been applied to the underlying writer
        writer.Flush()
    }

    Here are examples of writing data to files using Go.

  • How Select on Channel Works in Golang

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        // in this example we will choose between two channels
        c1 := make(chan string)
        c2 := make(chan string)
    
        go func() {
            time.Sleep(1 * time.Second)
            c1 <- "one"
        }()
    
        // each channel will receive a value after some time
        go func() {
            time.Sleep(2 * time.Second)
            c2 <- "two"
        }()
    
        // use select statement to wait for both values ​​at the same time,
        // printing each one as it arrives
        for i := 0; i < 2; i++ {
            select {
            case msg1 := <-c1:
                fmt.Println("received", msg1)
            case msg2 := <-c2:
                fmt.Println("received", msg2)
            }
        }
    }

    Select statement allows you to wait for multiple operations on a channel.

  • Worker pools in Go

    package main
    
    import (
        "fmt"
        "time"
    )
    
    // this is a worker that we will run in several parallel instances
    // these workers will receive tasks through the 'jobs' channel and send the results to 'results'
    // we will wait for one second for each task to simulate heavy requests
    func worker(id int, jobs <-chan int, results chan<- int) {
        for j := range jobs {
            fmt.Println("worker", id, "started job", j)
            time.Sleep(time.Second)
            fmt.Println("worker", id, "finished job", j)
            results <- j * 2
        }
    }
    
    func main() {
        // to use worker pool, we need to send a task and receive the execution results
        // therefore 2 channels are created
        jobs := make(chan int, 100)
        results := make(chan int, 100)
    
        // start 3 workers, initially blocked because no assignments yet
        for w := 1; w <= 3; w++ {
            go worker(w, jobs, results)
        }
    
        // send 5 jobs and then close the channel, notofying that all jobs have been sent
        for j := 1; j <= 5; j++ {
            jobs <- j
        }
    
        close(jobs)
    
        // collect all the results
        // this also ensures that the goroutines have ended
        for a := 1; a <= 5; a++ {
            <-results
        }
    }

    This example shows how to implement a worker pool using channels and goroutines.

  • Channel synchronization in Go

    package main
    
    import (
        "fmt"
        "time"
    )
    
    // the channel is used to notify main goroutine that the function completed successfully
    func worker(done chan bool) {
        fmt.Println("working...")
        time.Sleep(time.Second)
        fmt.Println("done")
    
        done <- true // send a value to indicate that the function completed successfully
    }
    
    func main() {
        done := make(chan bool, 1) // create a buffered channel for notification
        go worker(done) // run worker
    
        <-done // blocked until a notification is received from the worker from the channel
               // if you remove the line <- done the program will close before the worker starts
    }

    Channels can be used to synchronize execution between goroutines. Here is an example of using a channel to wait for a goroutine to complete.

  • Check if a file exists or not in Go

    package main
    
    import (
    	"errors"
    	"fmt"
    	"os"
    )
    
    func main() {
    	filePath := "file.txt"
    
    	if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
    		fmt.Println("file not exists")
    	} else {
    		fmt.Println("file exists")
    	}
    }

    To check if a certain file exists inside a given directory in Golang, the Stat() and isNotExists() functions from the os package can be used. The Stat() function is used to return a file information structure that describes the file.