Using gofmt to Format Code

For a programming language, code formatting is often a contentious issue. Different developers may have varying coding styles and habits. However, if all developers can use a unified format to write code, they can focus their valuable time on solving the problems the language is meant to address. Introduction to gofmt The Golang development team has established an official code style and introduced the gofmt tool (gofmt or go fmt) to help developers format their code to this unified style. gofmt is a CLI program that prioritizes reading from standard input. If a file path is provided, it will format that file. If a directory is provided, it will format all .go files in the directory. If no parameters are provided, it will format all .go files in the current directory. ...

July 17, 2018 · 4 min · Zhiya

Troubleshooting DNS Resolution Issues When Setting Up a Kubernetes Cluster

Problem Description While setting up a Kubernetes cluster and installing the kube-dns plugin, I ran an Ubuntu container and found that it couldn’t resolve domain names outside the cluster. Initially, it could resolve domain names within the cluster, but after some time, it couldn’t even resolve those. $ nslookup kubernetes.default Server: 10.99.0.2 Address 1: 10.99.0.2 kube-dns.kube-system.svc.cluster.local nslookup: can't resolve 'kubernetes.default' Troubleshooting Process Before troubleshooting, let’s consider the DNS resolution process in a Kubernetes cluster. In a cluster with kube-dns installed, the dnsPolicy attribute of a regular Pod is set to the default value ClusterFirst, meaning it points to the internal DNS server of the cluster. Kube-dns is responsible for resolving internal domain names, and the dnsPolicy value of the kube-dns Pod is Default, meaning it inherits the DNS server from the Node it resides on. For unresolved external domain names, kube-dns queries the external DNS server, as illustrated in the diagram. ...

July 15, 2018 · 5 min · Zhiya

Installing Golang Environment and Dependency Management

In 2015, Go 1.5 introduced an experimental vendor mechanism (which became enabled by default in Go 1.6 in 2016). The vendor mechanism involves adding a vendor folder to the project to store dependencies, allowing different projects to isolate their dependencies. Golang is a statically typed, compiled, concurrent programming language with garbage collection. Golang provides convenient installation packages, supporting Windows, Linux, and Mac systems. Download and Install Package The official Golang website is https://golang.org/. If the official site is inaccessible, you can visit https://golang.google.cn/. On the website, clicking on Download Go will take you to the download page, where installation packages for various systems are available, along with source code for compiling and installation. ...

July 10, 2018 · 3 min · Zhiya

The Evolution of Python Function Parameters You Didn't Know About

The mechanism for handling function parameters is a crucial aspect of Python. As Python has evolved, the flexibility and richness of parameter handling have continuously increased, allowing us to write simplified code and handle complex calls. Keyword Arguments Specify the parameter’s name when calling, matching the parameter name in the function declaration. Keyword arguments are the most basic and common in Python functions. Let’s write a bookkeeping function with parameters for the date and amount to be recorded. ...

July 10, 2018 · 5 min · Zhiya

The Secrets of Python You Didn't Know | String Concatenation

String concatenation is the process of merging two or more strings into one. While it may seem like a basic problem, in Python, there are multiple ways to achieve string concatenation. A poor choice can lead to performance issues. Method 1: Plus Operator Many languages support using the plus operator for string concatenation, and Python is no exception. Simply add two or more strings together to concatenate them. a = 'Python' b = 'Cuisine' r = a + b # Outputs 'PythonCuisine' Method 2: Using the % Operator Before Python 2.6, the % operator was the only way to format strings, and it can also be used for string concatenation. ...

June 28, 2018 · 3 min · Zhiya