-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.go
More file actions
39 lines (32 loc) · 892 Bytes
/
Copy pathmessage.go
File metadata and controls
39 lines (32 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package chat_application
import (
"fmt"
"time"
)
type Message struct {
sender string
timestamp string
msg []byte
}
type TimestampedMessage struct {
timestamp string
msg []byte
}
func NewMessage(sender string, msg []byte) *Message {
return &Message{sender: sender, timestamp: time.Now().Format(time.RFC3339), msg: msg}
}
func (m *Message) ToBytes() []byte {
sendMsg := []byte{}
sendMsg = append(sendMsg, []byte(fmt.Sprintf("[%s] %s: ", m.timestamp, m.sender))...)
sendMsg = append(sendMsg, m.msg...)
return sendMsg
}
func NewTimestampedMessage(msg []byte) *TimestampedMessage {
return &TimestampedMessage{timestamp: time.Now().Format(time.RFC3339), msg: msg}
}
func (m *TimestampedMessage) ToBytes() []byte {
sendMsg := []byte{}
sendMsg = append(sendMsg, []byte(fmt.Sprintf("[%s] ", m.timestamp))...)
sendMsg = append(sendMsg, m.msg...)
return sendMsg
}