Skip to content

Commit 3e709ab

Browse files
committed
fmt
1 parent 53b7ff9 commit 3e709ab

File tree

2 files changed

+44
-49
lines changed

2 files changed

+44
-49
lines changed

alink.go

+21-23
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818
)
1919

2020
// GetBytesReaderWithIoReader create a new bytes reader
21-
func GetBytesReaderWithIoReader(respBody io.Reader)(reader *bytes.Reader ,err error){
21+
func GetBytesReaderWithIoReader(respBody io.Reader) (reader *bytes.Reader, err error) {
2222

2323
c, err := ioutil.ReadAll(respBody)
24-
if err == nil{
24+
if err == nil {
2525
reader = bytes.NewReader(c)
2626
}
2727
return reader, err
@@ -34,7 +34,6 @@ func GetByteWithIoReader(respBody io.Reader) ([]byte, error) {
3434
return b, err
3535
}
3636

37-
3837
// GetByteReader use bytes.NewReader create a new reapBody to read
3938
func GetByteReader(respBody []byte) *bytes.Reader {
4039
reader := bytes.NewReader(respBody)
@@ -141,7 +140,7 @@ func TitleBytes(httpBody *bytes.Reader) (t string, err error) {
141140
// GetTitleWithByte
142141
func GetTitleWithByte(httpBody []byte) (t string, err error) {
143142
title := ""
144-
body:= GetByteReader(httpBody)
143+
body := GetByteReader(httpBody)
145144

146145
node, err := html.Parse(body)
147146
if err != nil {
@@ -154,47 +153,47 @@ func GetTitleWithByte(httpBody []byte) (t string, err error) {
154153
}
155154

156155
// GetImgSrcWithBytesReader get all img urls
157-
func GetImgSrcWithBytesReader(httpBody *bytes.Reader )(i *[]string, err error){
158-
ul:= []string{}
159-
page,err := html.Parse(httpBody)
160-
if err != nil{
161-
return &ul,err
156+
func GetImgSrcWithBytesReader(httpBody *bytes.Reader) (i *[]string, err error) {
157+
ul := []string{}
158+
page, err := html.Parse(httpBody)
159+
if err != nil {
160+
return &ul, err
162161
}
163-
ll , _ := getImgUrl(page,&ul)
164-
return ll,nil
162+
ll, _ := getImgUrl(page, &ul)
163+
return ll, nil
165164

166165
}
167166

168167
// GetImgSrcWithByte
169-
func GetImgSrcWithByte(httpBody []byte )(i *[]string, err error){
168+
func GetImgSrcWithByte(httpBody []byte) (i *[]string, err error) {
170169
var ul []string
171170
mm := GetByteReader(httpBody)
172171

173-
page,err := html.Parse(mm)
174-
if err != nil{
175-
return &ul,err
172+
page, err := html.Parse(mm)
173+
if err != nil {
174+
return &ul, err
176175
}
177-
ll , _ := getImgUrl(page,&ul)
178-
return ll,nil
176+
ll, _ := getImgUrl(page, &ul)
177+
return ll, nil
179178

180179
}
181180

182181
// getImgUrl
183182
func getImgUrl(node *html.Node, ad *[]string) (l *[]string, b bool) {
184183
flag := false
185-
if isImgElement(node){
186-
for _, v := range node.Attr{
184+
if isImgElement(node) {
185+
for _, v := range node.Attr {
187186
if v.Key == "src" {
188187
if check(ad, v.Val) == false {
189188
*ad = append(*ad, v.Val)
190189
}
191190
}
192191
}
193-
return ad ,true
192+
return ad, true
194193
}
195194

196-
for p:= node.FirstChild;p!=nil;p= p.NextSibling{
197-
ul,f := getImgUrl(p,ad)
195+
for p := node.FirstChild; p != nil; p = p.NextSibling {
196+
ul, f := getImgUrl(p, ad)
198197
if f {
199198
flag = f
200199
ad = ul
@@ -228,7 +227,6 @@ func GetHrefWithByte(httpBody []byte) (l *[]string, err error) {
228227
return ff, nil
229228
}
230229

231-
232230
// getHref get url
233231
func getHref(node *html.Node, h *[]string) (f *[]string, n bool) {
234232
b := false

alink_test.go

+23-26
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,34 @@ func TestGetImgSrcWithBytesReader(t *testing.T) {
8686
httpBody *bytes.Reader
8787
}
8888

89-
9089
var html = `<a href="http://jjjj.com">1</a> <video src="http://abc.com/ab.mp4">
9190
<a style=\"\" href=http://imgur.com>3</a> <img src="abc.com/img.jpg">http://alink.com</p>`
9291

9392
c := []byte(html)
94-
i := args{
93+
i := args{
9594
bytes.NewReader(c),
9695
}
9796

9897
var html1 = `<a href="http://jjjj.com">1</a> <video src="http://abc.com/ab.mp4">
9998
<a style=\"\" href=http://imgur.com>3</a> <img lin="abc.com/img.jpg">http://alink.com</p>`
10099

101100
c1 := []byte(html1)
102-
i1 := args{
101+
i1 := args{
103102
bytes.NewReader(c1),
104103
}
105104

106-
var tests =[]struct {
107-
name string
108-
args args
109-
wantS *[]string
105+
var tests = []struct {
106+
name string
107+
args args
108+
wantS *[]string
110109
wantErr bool
111110
}{
112-
{"img",i,&[]string{"abc.com/img.jpg"},false },
113-
{"imgNoSrc",i1,&[]string{},false },
111+
{"img", i, &[]string{"abc.com/img.jpg"}, false},
112+
{"imgNoSrc", i1, &[]string{}, false},
114113
}
115-
for _, tt :=range tests{
116-
t.Run(tt.name,func(t *testing.T){
117-
gotS ,err:= GetImgSrcWithBytesReader(tt.args.httpBody)
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
gotS, err := GetImgSrcWithBytesReader(tt.args.httpBody)
118117
if (err != nil) != tt.wantErr {
119118
t.Errorf("GetImgSrcWithBytesReader() error = %v, wantErr %v", err, tt.wantErr)
120119
return
@@ -238,8 +237,6 @@ func BenchmarkAlink(b *testing.B) {
238237
}
239238
}
240239

241-
242-
243240
func TestGetImgSrcWithByte(t *testing.T) {
244241
type args struct {
245242
httpBody []byte
@@ -250,16 +247,16 @@ func TestGetImgSrcWithByte(t *testing.T) {
250247
http://alink.com
251248
</p>`
252249

253-
//string to byte.reader
254-
httpBody :=args{[]byte(reader) }
250+
//string to byte.reader
251+
httpBody := args{[]byte(reader)}
255252

256-
tests := []struct {
253+
tests := []struct {
257254
name string
258255
args args
259256
wantI *[]string
260257
wantErr bool
261258
}{
262-
{"img",httpBody, &[]string{"http://jjjj.com"},false},
259+
{"img", httpBody, &[]string{"http://jjjj.com"}, false},
263260
}
264261

265262
for _, tt := range tests {
@@ -294,7 +291,7 @@ func TestGetTitleWithByte(t *testing.T) {
294291
wantErr bool
295292
}{
296293
// TODO: Add test cases.
297-
{"title",a1,"test1",false},
294+
{"title", a1, "test1", false},
298295
}
299296
for _, tt := range tests {
300297
t.Run(tt.name, func(t *testing.T) {
@@ -315,7 +312,7 @@ func TestGetByteWithIoReader(t *testing.T) {
315312
respBody io.Reader
316313
}
317314

318-
af :=[]byte("abc")
315+
af := []byte("abc")
319316

320317
body := args{
321318
bytes.NewReader(af),
@@ -327,7 +324,7 @@ func TestGetByteWithIoReader(t *testing.T) {
327324
wantErr bool
328325
}{
329326
// TODO: Add test cases.
330-
{"readToByte",body,[]byte{97,98,99},false},
327+
{"readToByte", body, []byte{97, 98, 99}, false},
331328
}
332329
for _, tt := range tests {
333330
t.Run(tt.name, func(t *testing.T) {
@@ -383,8 +380,8 @@ func TestGetHrefWithByte1(t *testing.T) {
383380
type args struct {
384381
httpBody []byte
385382
}
386-
html :=`<p>test<a href="test.com">one</a></p>`
387-
html1 :=`<p>test <a href="#">one</a></p>`
383+
html := `<p>test<a href="test.com">one</a></p>`
384+
html1 := `<p>test <a href="#">one</a></p>`
388385

389386
h := []byte(html)
390387
h1 := []byte(html1)
@@ -399,8 +396,8 @@ func TestGetHrefWithByte1(t *testing.T) {
399396
wantErr bool
400397
}{
401398
// TODO: Add test cases.
402-
{"one",arr,&[]string{"test.com"},false},
403-
{"two",arr1,&[]string{""},false},
399+
{"one", arr, &[]string{"test.com"}, false},
400+
{"two", arr1, &[]string{""}, false},
404401
}
405402
for _, tt := range tests {
406403
t.Run(tt.name, func(t *testing.T) {
@@ -414,4 +411,4 @@ func TestGetHrefWithByte1(t *testing.T) {
414411
}
415412
})
416413
}
417-
}
414+
}

0 commit comments

Comments
 (0)