西维蜀黍

【MySQL】Establishing a Database Connection

  ...


【Golang】Set实现

Interface

type Set interface {
	// Adds an element to the set. Returns whether
	// the item was added.
	Add(i interface{}) bool

	// Returns the number of elements in the set.
	Cardinality() int

	// Removes all elements from the set, leaving
	// the empty set.
	Clear()

	// Returns a clone of the set using the same
	// implementation, duplicating all keys.
	Clone() Set

	// Returns whether the given items
	// are all in the set.
	Contains(i ...interface{}) bool

	// Returns the difference between this set
	// and other. The returned set will contain
	// all elements of this set that are not also
	// elements of other.
	//
	// Note that the argument to Difference
	// must be of the same type as the receiver
	// of the method. Otherwise, Difference will
	// panic.
	Difference(other Set) Set

	// Determines if two sets are equal to each
	// other. If they have the same cardinality
	// and contain the same elements, they are
	// considered equal. The order in which
	// the elements were added is irrelevant.
	//
	// Note that the argument to Equal must be
	// of the same type as the receiver of the
	// method. Otherwise, Equal will panic.
	Equal(other Set) bool

	// Returns a new set containing only the elements
	// that exist only in both sets.
	//
	// Note that the argument to Intersect
	// must be of the same type as the receiver
	// of the method. Otherwise, Intersect will
	// panic.
	Intersect(other Set) Set
	
	...
}	
  ...


【Golang】go-redis 连接Redis

go-redis

client := redis.NewClient(&redis.Options{
	Addr:     "localhost:6379",
	Password: "", // password
	DB:       0,  // use default DB
})

pong, err := client.Ping().Result()
fmt.Println(pong, err)
var id uint64 = 64
rule := &Rule{
	Id: &id,
}
b, err := json.Marshal(rule)
if err != nil {
	panic(err)
}

fmt.Println(client.Set("cache1", b, 0).Result())

a, err := client.Get("cache1").Result()

var item myProto.CompositeLogisticPromotionRule
err = json.Unmarshal([]byte(a), &item)
if err != nil {
	panic(err)
}
fmt.Println(item.Id)
  ...


【Network】通过反向代理实现内网穿透

Background

树莓派通常会(通过LAN 或者 WIFI)连接到一个路由器上,如果我们不方便在路由器上配置端口映射(或者这个路由器之外还有另外的 NAT),当我们不在这个内网时,就无法管理我们的树莓派。

这时候,我们可以考虑使用用于内网穿透的反向代理应用。

如果你有一个拥有公网 IP 的机器,比如云主机、VPS 等。那么最稳定的方式就是在这个机器上自己架设中转节点。

  ...


【Golang】内置函数

当我们想要在 Go 语言中初始化一个结构时,可能会用到两个不同的关键字 — makenew。因为它们的功能相似,所以初学者可能会对这两个关键字的作用感到困惑,但是它们两者能够初始化的却有较大的不同。

  • make 的作用是初始化内置的数据结构,包括且仅包括 slice、map 和 Channel
  • new 的作用是根据传入的类型分配一片内存空间,并返回指向这片内存空间的指针

make()

slice := make([]int, 0, 100)
hash := make(map[int]bool, 10)
ch := make(chan int, 5)
  1. slice 是一个包含 datacaplen 的结构体 reflect.SliceHeader
  2. hash 是一个指向 runtime.hmap 结构体的指针;
  3. ch 是一个指向 runtime.hchan 结构体的指针;
  ...