-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASN.1.go
84 lines (68 loc) · 1.66 KB
/
ASN.1.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
/*
ASN.1
本代码测试ASN1的marshal和unmarsshal方法。
分别对int,string,time进行marshal/unmarshal操作。
*/
package main
import (
"encoding/asn1"
"fmt"
"os"
"time"
)
func main() {
test_int()
test_string()
test_time()
test_time2()
}
func test_int() {
a := 13
fmt.Println("Input: ", a)
mdata, err := asn1.Marshal(a)
checkError(err)
var n int
_, err1 := asn1.Unmarshal(mdata, &n)
checkError(err1)
fmt.Println("After marshal/unmarshal: ", n)
fmt.Println("=================================================")
}
func test_string() {
a := "hello world"
fmt.Println("Input: ", a)
mdata, err := asn1.Marshal(a)
checkError(err)
var newstr string
_, err1 := asn1.Unmarshal(mdata, &newstr)
checkError(err1)
fmt.Println("After marshal/unmarshal: ", newstr)
fmt.Println("=================================================")
}
func test_time() {
a := time.Now()
fmt.Println("Input: ", a)
mdata, err := asn1.Marshal(a)
checkError(err)
var newtime = new(time.Time)
_, err1 := asn1.Unmarshal(mdata, newtime)
checkError(err1)
fmt.Println("After marshal/unmarshal: ", newtime)
fmt.Println("=================================================")
}
func test_time2() {
a := time.Now()
fmt.Println("Input: ", a)
mdata, err := asn1.Marshal(a)
checkError(err)
var newtime time.Time // 定义变量,不创建instance
_, err1 := asn1.Unmarshal(mdata, &newtime) // 需要 &
checkError(err1)
fmt.Println("After marshal/unmarshal: ", newtime)
fmt.Println("=================================================")
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}