V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Zeffee

Golang 写一个简易版多人聊天室,望指点

  •  
  •   Zeffee · Apr 25, 2016 · 2189 views
    This topic created in 3662 days ago, the information mentioned may be changed or developed.

    1.新手一枚,用空余时间写了个简易的多人聊天程序。写完但不知代码质量如何,望大牛们能指点下可改进的地方。

    附上地址: https://github.com/sostuts/Chat

    2.另外,请教下 tcp 接收信息的时候,除了 length,_:= net.Conn.Read() 可以获取到信息长度,还有其他方法吗?

    7 replies    2016-05-08 19:38:53 +08:00
    chzyer
        1
    chzyer  
       Apr 25, 2016
    最常见都是通过 length + payload 来做协议的,而且 Write() 一次有可能需要多次 Read() 才能读完,所以一般的接收端代码是这样的

    var length int32
    binary.Read(conn, binary.Bigdian, &length)
    payload := make([]byte, int(length))
    io.ReadFull(conn, payload)
    Zeffee
        2
    Zeffee  
    OP
       Apr 25, 2016 via Android
    没人回😳
    Zeffee
        3
    Zeffee  
    OP
       Apr 25, 2016 via Android
    @chzyer 我回去试下, thx👌
    susanDao
        4
    susanDao  
       Apr 26, 2016
    刚上手 go ,学习之。。
    Zeffee
        5
    Zeffee  
    OP
       Apr 26, 2016
    @susanDao 我也是接触 Go 差不多两周时间,,感觉 Go 还是挺不错的
    oreo
        6
    oreo  
       May 7, 2016
    可以,正好没东西.
    Zeffee
        7
    Zeffee  
    OP
       May 8, 2016 via Android
    @oreo ???
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   880 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 22:53 · PVG 06:53 · LAX 15:53 · JFK 18:53
    ♥ Do have faith in what you're doing.