Sign in

Chances are if you’ve written some go or delved into a well-maintained go project, you’ve seen structs with an underscore field:

type ListTablesInput struct {
_ struct{} `type:"structure"`
ExclusiveStartTableName *string `min:"3" type:"string" Limit *int64 `min:"1" type:"integer"`
}

> https://github.com/aws/aws-sdk-go/blob/7e0246814d8eb87bdc6850c44e8f75f020db80eb/service/dynamodb/api.go#L15104-L15115

The purpose behind using the _ field on a struct is to enforce keyed fields when initializing a struct.

input := ListTablesInput{
ExclusiveStartTableName: "table-name",
Limit: 100,
}
// instead of
input := ListTablesInput{"table-name", 100}

If you attempt to initialize a struct without the field names, you will get an error along the lines of `cannot use as type struct {} in…


  1. given a composite index, make sure the order of your index is in line with your access pattern. you do not want the following

If you’ve ever seen something like this and wondered what the hell this means you’re in luck.

An SQL index lookup requires (1) the tree traversal (2) following the linked list node chain (3) fetching the table data. In step (1) a query will traverse a B-tree to quickly find the page that holds the row(s). The rows themselves are nodes in a doubly linked list and the database will perform step (2) to find all entries that match the key. And lastly the database will use the row IDs found to get the records themselves.

Bitmap Index Scan means…


Terminology:

  • Boxes are individual squares
  • Units are complete rows, columns, and 3x3 squares
  • Peers are boxes that belong to the same unit
rows = 'ABCDEFGHI'
cols = '123456789'
def cross(a, b):
# output = []
# for s in a:
# for t in b:
# output.append(s+t)
# return output
return [s+t for s in a for t in b]
boxes = cross(rows, cols)
row_units = [cross(r, cols) for r in rows]
column_units = [cross(rows, c) for c in cols]
square_units = [cross(rs, cs) for rs in ('ABC','DEF','GHI') for cs in ('123','456','789')]
unitlist = row_units + column_units + square_units
units = dict((s, [u for u…

$ brew tap universal-ctags/universal-ctags$ brew install --HEAD universal-ctags$ alias ctags=“`brew —-prefix`/bin/ctags”

Using tags is a powerful tool to be able to jump to the definition of a function, variable, or class in Vim. Install universal-ctags and alias it as ctags. This allows adding support for languages that are not supported out of the box by universal-ctags (such as golang).

Add the following lines to your .ctags file to support golang:

--langdef=golang
--langmap=golang:.go
--regex-golang=/func([ \t]+\([^)]+\))?[ \t]+([a-zA-Z0-9_]+)/\2/f,func/
--regex-golang=/var[ \t]+([a-zA-Z_][a-zA-Z0-9_]+)/\1/v,var/

Now you can generate tags for source files:

$ ctags -R .

Open up a file in vim and use Ctrl + ] to jump to the definition of whatever is on your cursor and Ctrl + t to go back.

:help ctags # for more information and tips

Supported ctags languages http://ctags.sourceforge.net/languages.html

http://ctags.sourceforge.net/EXTENDING.html


Most times when writing software, you use other software or as we call them “dependencies.” In golang, we may call that a collection of go packages. That collection of go packages is outlined in a file called go.mod.

module example.com/hellorequire (github.com/aws/aws-lambda-go v1.13.3 // indirectgithub.com/aws/aws-sdk-go v1.25.45github.com/bitly/go-simplejson v0.5.0 // indirectgithub.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869golang.org/x/crypto v0.0.0-20191202143827-86a70503ff7e // indirectgolang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirectgolang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirectgolang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 // indirectgolang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirectgolang.org/x/tools v0.0.0-20191202203127-2b6af5f9ace7 // indirectgoogle.golang.org/genproto v0.0.0-20191115221424-83cc0476cb11 // indirectgoogle.golang.org/grpc v1.28.0gopkg.in/square/go-jose.v2 v2.4.0 // indirectgopkg.in/yaml.v2 v2.2.7 …

Chinese Finger Trap
func Google(query string) (results []string) {
c := make(chan string)

go func() {
c <- Web(query)
}()
go func() {
c <- Image(query)
}()
go func() {
c <- Video(query)
}()
timeout := time.After(80 * time.Millisecond)
for i := 0; i < 3; i++ {
select {
case result := <-c:
results = append(results, result)
case <-timeout:
fmt.Println("timed out")
return
}
}
return
}

Let’s take a look at an example from Google I/O talk given by Rob Pike. …


type Result struct {
Item item
Error error
}
ch := make(chan Result)var wg sync.WaitGroupfor _, i:= range items { go func(i Item) {
defer wg.Done()

res, err := processItem(i)
if err != nil {
ch <- Result{Item: i, Error: err}
return
}
ch <- Result{Item: res, Error: err}
}(i)
}
go func() {
wg.Wait()
close(ch)
}
var successful, failed []Itemfor r := range ch {
if r.Error != nil {
failed = append(failed, r.Item)
return
}
succeeded = append(succeeded, r.Item)
}

Asynchronously wg.Wait(), so that the channel values are processed and appended to appropriate slices without…


Photo by Alice on Unsplash

If you’ve been to China or have Chinese friends you know that WeChat is used for just about anything in China. WeChat has an estimated 900 million monthly active users and the vast majority of the country uses WeChat Pay. WeChat Pay allows users to pay virtually anything with the money linked to their WeChat. This is more than just your usual Apple Pay, because unlike America where every store that wants to adopt Apple Pay needs a corresponding point of sale system, Chinese vendors can use an offline QR code linked to WeChat and bank account.

This is great…

Paul Yeo

disscepolo della sperientia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store