go codelet

菲葑不棄,敝帚自珍

接口

package main
import(
    "fmt"
)
type SendAlgorithm interface{
    OnDataAvailable(byte uint32)
    GetCongestionWindow() uint64
    OnPacketLost()
}
const MinimalWindow uint64=1
const InitialWindow uint64=12
const SSthresh uint64 =30
const Mss uint64 =1500
type CubicSender struct{
    CongestionWindow uint64
    CwndCnt uint64
    total uint64
    alpha float32
    beta float32
}
func NewCubicSender(a float32,b float32) SendAlgorithm{
    return &CubicSender{
    CongestionWindow:InitialWindow,
    alpha:a,
    beta:b,
    }
}
func (c*CubicSender)OnDataAvailable(byte uint32){
    c.total+=uint64(byte)
    c.CwndCnt=uint64(c.total/Mss)
    if c.CwndCnt>=c.CongestionWindow{
    c.total=0;
    c.CongestionWindow++
    }
    fmt.Printf("%f,%f\n",c.alpha,c.beta)
}
func (c*CubicSender) GetCongestionWindow() uint64{
    return c.CongestionWindow
}
func (c*CubicSender) OnPacketLost(){
    var cwnd float32 =float32(c.CongestionWindow)*c.beta
    c.CongestionWindow=uint64(cwnd)
    c.total=0
}
// just some simulation  with different a ,b
func NewRenoSender(a float32,b float32) SendAlgorithm{
    return &CubicSender{
    CongestionWindow:InitialWindow,
    alpha:a,
    beta:b,
    }
}
type State int
const (
    TCP_RENO State = iota // value --> 0
    TCP_CUBIC              // value --> 1
)
type PathManager struct{
    senders map[uint32]interface{}
}
func (pm*PathManager) SetUp(id uint32,tcp State,p*Path){
    var cong SendAlgorithm
    switch(tcp){
    case TCP_RENO:
    cong=NewRenoSender(1,0.5)
    case TCP_CUBIC:
    cong=NewRenoSender(1,0.7)
    default:
    cong=NewRenoSender(1,0.5)   
    }
    p.sendAlgorithm=cong
    pm.senders[id]=cong
    p.SetUp(pm.senders);
}
type Path struct{
    senders map[uint32]interface{}
    sendAlgorithm SendAlgorithm
}
func (p*Path)SetUp(senders map[uint32]interface{}){
    p.senders=senders
}
func (p*Path) OndataAvailble(byte uint32){
    p.sendAlgorithm.OnDataAvailable(byte)
}
func (p*Path) OnPacketLost(){
    p.sendAlgorithm.OnPacketLost()
}
func (p*Path) GetTotalCwnd() uint64{
    var totalcwnd uint64 
    for _,s:=range p.senders{
    var cubicSender SendAlgorithm=s.(*CubicSender)//this is ok
    //var cubicSender SendAlgorithm=s.(SendAlgorithm)//this is ok
    //var cubicSender SendAlgorithm=SendAlgorithm(s) this is not work
    //cwnd:=s.(SendAlgorithm).GetCongestionWindow()   this is ok
    cwnd:=cubicSender.GetCongestionWindow()
    totalcwnd+=cwnd
    }
    return totalcwnd
}
func (p*Path) GetPathCwnd() uint64{
    return p.sendAlgorithm.GetCongestionWindow()
}
func main(){
    pm:=new(PathManager)
    pm.senders=make(map[uint32]interface{})
    p1:=new(Path)
    p2:=new(Path)
    pm.SetUp(1,TCP_CUBIC,p1)
    pm.SetUp(2,TCP_RENO,p2)
    total1:=p1.GetTotalCwnd()
    total2:=p2.GetTotalCwnd()
    fmt.Printf("%d,%d\n",total1,total2)
    pathcwnd:=p1.GetPathCwnd()
    fmt.Printf("%d\n",pathcwnd)
}

獲取系統(tǒng)時間(毫秒)

package main

import 
(
    "fmt"
    "flag"
    "time"
)
type Config struct{
    CreatePath *bool
}
func Test(config *Config){
    fmt.Println(*config.CreatePath);
}
func GetMilliSeconds() int64{
    now := time.Now()
    nanos := now.UnixNano()
    millis := nanos / 1000000
    return millis
}
func main(){
    var millis  int64=GetMilliSeconds()
    verbose := flag.Bool("v", false, "verbose")
    multipath := flag.Bool("m", false, "multipath")
    output := flag.String("o", "out.txt", "logging output")
    flag.Parse()
    fmt.Println(*verbose)
    fmt.Println(*multipath)
    fmt.Println(*output)
    var config *Config=new(Config)
    config.CreatePath=multipath;
    Test(config)
    var delta int64=GetMilliSeconds()-millis;
    fmt.Println(delta);
}

thus not work.
var config Config=new Config{
CreatePath:
multipath,
})

multi value in single value context

package main

import 
(
    "fmt"
    "github.com/emirpasic/gods/lists/arraylist"
    "github.com/emirpasic/gods/utils"
)

func first(args ...interface{})interface{} {
    return args[0]
}
func main(){

    list := arraylist.New()
    list.Add("abc")                         // ["a"]
    list.Add("c", "b")                    // ["a","c","b"]
    list.Sort(utils.StringComparator)     // ["a","b","c"]
    var ele string
    ele=first(list.Get(0)).(string)
    fmt.Println(ele);
}

ele,=list.Get(0), error, since the first return value is a interface.
but ele,
=list.Get(0) would be ok.
interface{} 即為引用

interface cast

import(
"fmt"
"unsafe"
)
    var  a int32
    a=123
    var b int64
    b=123
    var c *int64
    c=&b;
    var d interface{}
    d=&b
    var e *int64;
    e=d.(*int64)
    fmt.Println("size a in main",unsafe.Sizeof(a))
    fmt.Println("size b in main",unsafe.Sizeof(b))
    fmt.Println("size c in main",unsafe.Sizeof(c))
    fmt.Println("size d in main",unsafe.Sizeof(d))
    fmt.Println("value e in main",*e)

interface function

package main
 
import (
    "fmt"
)
type StreamSender  interface{
    onDataAvailable(int32)
}
func Test(sender StreamSender){
    sender.onDataAvailable(32);
}
type Session struct{
    id int32
}
func (s *Session)onDataAvailable(len int32){
    fmt.Printf("sesion %d data len %d\n",s.id,len)
}

func main(){
    s:=new(Session);
    s.id=1;
    Test(s)
}

interface{} to string

package main 
import(
    "fmt"
    "strings"
)
func GetString(value interface{}) int{
    return strings.Count(value.(string),"")-1
}
func main(){
    s:="hello world"
    fmt.Printf("length %d\n",GetString(s))
}

回調(diào)

package main
import(
    "fmt"
)
type stream struct{
    id int
    OnIncomingData func(len int)
}

func newStream(id int, cb func(len int)) *stream{
    return &stream{
        id:id,
        OnIncomingData:cb,
    }
}

func OnIncomingData(len int){
    fmt.Printf("incoming data len %d\n",len)
}

func main(){
    stream:=newStream(1,OnIncomingData)
    stream.OnIncomingData(1400)
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市读规,隨后出現(xiàn)的幾起案子摇展,更是在濱河造成了極大的恐慌硫惕,老刑警劉巖叁巨,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件财岔,死亡現(xiàn)場離奇詭異避诽,居然都是意外死亡龟虎,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門沙庐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鲤妥,“玉大人,你說我怎么就攤上這事拱雏∶薨玻” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵铸抑,是天一觀的道長贡耽。 經(jīng)常有香客問我,道長鹊汛,這世上最難降的妖魔是什么菇爪? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮柒昏,結(jié)果婚禮上凳宙,老公的妹妹穿的比我還像新娘。我一直安慰自己职祷,他們只是感情好氏涩,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著有梆,像睡著了一般是尖。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上泥耀,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天饺汹,我揣著相機與錄音,去河邊找鬼痰催。 笑死兜辞,一個胖子當著我的面吹牛迎瞧,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播逸吵,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼凶硅,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了扫皱?” 一聲冷哼從身側(cè)響起足绅,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎韩脑,沒想到半個月后氢妈,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡段多,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年允懂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衩匣。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡蕾总,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出琅捏,到底是詐尸還是另有隱情生百,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布柄延,位于F島的核電站蚀浆,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏搜吧。R本人自食惡果不足惜市俊,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望滤奈。 院中可真熱鬧摆昧,春花似錦、人聲如沸蜒程。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昭躺。三九已至忌锯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間领炫,已是汗流浹背偶垮。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人似舵。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓脚猾,卻偏偏與公主長得像,于是被迫代替她去往敵國和親啄枕。 傳聞我的和親對象是個殘疾皇子婚陪,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355