判断两种末尾不同的长字符串,在使用正则表达式的基础上,进一步利用好字符串的方法,最后成功对问题进行解决。
1 package utils 2 3 import ( 4 "io/ioutil" 5 "os" 6 "regexp" 7 "strings" 8 ) 9 10 //IsLICENSE return true when file is right LICENSE format while return false when the file is wrong format 11 func IsLICENSE(filepath string, fileContext string) (bool, error) { 12 _, err := os.Open(filepath) 13 if err != nil { 14 return false, err 15 } 16 buff, err := ioutil.ReadFile(filepath) 17 text := string(buff) 18 19 reg := regexp.MustCompile(`12. Identification: [a-z0-9]+\n$`) 20 //Get the stand LICENSE string 21 license := reg.FindAllString(text, -1) 22 license1 := reg.FindAllString(fileContext, -1) 23 24 if license1 == nil { 25 return false, nil 26 } 27 28 str := strings.Replace(text, license[0], ``, -1) 29 str1 := strings.Replace(fileContext, license1[0], ``, -1) 30 31 if str != str1 { 32 return false, nil 33 } 34 return true, nil 35 }