Golang Practice

Advanced Golang Learning Memo Nothing is better than learning from real project! Struct, Method and Interface Struct The struct works as complex variable in GO. It’s a combination of different vars, func, and method. The source struct in pagerduty package for ListIncidentsOptions is like : type Client struct { debugFlag *uint64 lastRequest *atomic.Value lastResponse *atomic.Value authToken string apiEndpoint string v2EventsAPIEndpoint string HTTPClient HTTPClient } type ListIncidentsOptions struct { Total bool `url:"total,omitempty"` Since string `url:"since,omitempty"` Until string `url:"until,omitempty"` DateRange string `url:"date_range,omitempty"` Statuses []string `url:"statuses,omitempty,brackets"` IncidentKey string `url:"incident_key,omitempty"` ServiceIDs []string `url:"service_ids,omitempty,brackets"` TeamIDs []string `url:"team_ids,omitempty,brackets"` UserIDs []string `url:"user_ids,omitempty,brackets"` Urgencies []string `url:"urgencies,omitempty,brackets"` TimeZone string `url:"time_zone,omitempty"` SortBy string `url:"sort_by,omitempty"` Includes []string `url:"include,omitempty,brackets"` } It can inherit or sub struct:

Continue reading

Golang Reflect

Reflect is a very useful built-in package, it contains some powerful funcs such as TypeOf, Kind, Value and etc. I’ll try to document some of them here for future reference. TypeOf Have you ever wondered how to show the type of a struct object in golang? There’s no other simplier way as how they do it in python using type(), instead you have to import Reflect and use TypeOf or Kind to get it done.

Continue reading

Golang Routine

https://books.studygolang.com/gopl-zh/ch8/ch8-04.html GO Routine is golang core advantage towards other launguages, it makes async programming much easier and deliver async vars so smoothly. Following example shows how chan and async work with its async vars: package main import ( "fmt" "time" ) func scheduledNotification(t time.Duration) <-chan struct{} { ch := make(chan struct{}, 1) //make a chan var only accept one input each time, others need to wait go func() { time.Sleep(t) ch <- struct{}{} //giving empty ch value, the actuall result is only to reflect time waited println.

Continue reading

Dynamic Programming (动态规划) 动态规划思想,是用已知的规律,从历史数据计算出未来的最优解,这种方法省去暴力算法中的不必要的样本空间,非常重要。 爬楼梯 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 示例 1: 输入:n = 2 输出:2 解释:有两种方法可以爬到楼顶。 1 阶 + 1 阶 2 阶 示例 2: 输入:n = 3 输出:3 解释:有三种方法可以爬到楼顶。 1 阶 + 1 阶 + 1 阶 1 阶 + 2 阶 2 阶 + 1 阶 提示: 1 <= n <= 45 解题思路 N级台阶的解来自之前N-1级台阶的各自解之和,可以尝试创建动态规划的转移方程。首先尝试罗列出部分基础数值: f(0)=1 //也可以不考虑0的情况,因为题目说了n>=1,这样的话,程序就应该从f[1]+f[2]开始 f(1)=1 f(2)=1+1=2 //全部一级级走(1)+ 只走2级(1) f(3)=1+1+1=3 //全部一级级走(1)+ 先2次一级再2级1次(1)+ 先2级1次再1级1次(1) f(4)=1+1+1+1+1=5 //全部一级级走(1)+ 先2次一级再2级2次(1)+ 先3次一级再2级1次(1) + 先2级1次再1级2次(1)+ 先2级1次再2级1次(1) .

Continue reading

Golang Memo

Memo for Golang learning Pointer Use var var_name *var-type to define pointer type, * marks this var is a pointer of a type. var p *int var fp *float32 The value &p will be a pointer’s memery address. p := 42 fmt.Printf("Pointer memory address: %x\n", &p ) To give pointer a memory value. i := 42 var p *int p = &i // var p *int = &i fmt.Printf("Pointer memory address: %x\n", &p ) Check out following example to better understand this:

Continue reading

Author's picture


Love coding and new technologies

Cloud Solution Consultant