/
log1.go
123 lines (96 loc) · 2.28 KB
/
log1.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"os"
"fmt"
"log"
// "bufio"
)
type parts struct {
Users string
}
type load struct {
Session_id string
Schedule_id string
Full_name string
Latitude string
Longitude string
Datetime string
Elapsedtime string
Msg string
Participants []parts
Url string
}
type Message struct {
Messagetype string
Broadcast string // Y
Key string
From string
To string
Payload load
}
//Shaper is an interface and has a single function Area that returns an int.
type Shaper interface {
Area() int
Shit() int
}
type Rectangle struct {
length, width int
}
//this function Area works on the type Rectangle and has the same function signature defined in the interface Shaper. Therefore, Rectangle now implements the interface Shaper.
func (r Rectangle) Area() int {
return r.length * r.width
}
func (r Rectangle) Shit() int {
return r.length + r.width
}
func main() {
r := Rectangle{length:5, width:3}
fmt.Println("Rectangle r details are: ", r)
fmt.Println("Rectangle r's area is: ", r.Area())
s := Shaper(r)
fmt.Println("Area of the Shape r is: ", s.Area())
fmt.Println("Area of the Shape r is: ", s.Shit())
return;
cfgFile := "file.txt"
f, err := os.OpenFile(cfgFile, os.O_CREATE| os.O_APPEND| os.O_RDWR, 0666) // For read access.
if err != nil {
checkError(err)
}
// var l int
myLog := log.New(f, "PREFIXIO ", log.Lshortfile |log.Ldate|log.Ltime );
// l, err = f.WriteString("HELLO THERE n");
// fmt.Printf("length is :%d\n", l);
if( err != nil ){
checkError2(err);
}
myLog.Println("from logger");
//myFileAppendLine(cfgFile, "HELLO THERE \n");
fmt.Printf("Flags:%q Prefix:%q\n", log.Flags(), log.Prefix() );
os.Exit(0);
}
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}
func checkError2(err error) {
if err != nil {
fmt.Println("Error ", err.Error())
}
}
func myFileAppendLine ( fName string, line string ) {
f, err := os.OpenFile(fName, os.O_CREATE|os.O_APPEND|os.O_WRONLY , 0644)
checkError(err)
/*
if( err != nil ) {
f, err = os.Create(fName)
checkError(err)
}else {
_, err = f.Seek(0, os.SEEK_END)
}
*/
//_, err = f.Seek(0, os.SEEK_END)
_, err = f.WriteString(line)
f.Close()
}