From 3f1174a5199ebd5fda73f9b38eb574bb9122698c Mon Sep 17 00:00:00 2001 From: xiaox0321 Date: Fri, 20 Oct 2017 15:58:03 +0800 Subject: [PATCH] Update version.go Optimize duplicate code --- utils/version/version.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/utils/version/version.go b/utils/version/version.go index b631dba7..c2b9f558 100644 --- a/utils/version/version.go +++ b/utils/version/version.go @@ -25,31 +25,25 @@ func Full() string { return version } -func Proto(v string) int64 { +func getSubVersion(v string, position int) int64 { arr := strings.Split(v, ".") if len(arr) < 3 { return 0 } - res, _ := strconv.ParseInt(arr[0], 10, 64) + res, _ := strconv.ParseInt(arr[position], 10, 64) return res } +func Proto(v string) int64 { + return getSubVersion(v, 0) +} + func Major(v string) int64 { - arr := strings.Split(v, ".") - if len(arr) < 3 { - return 0 - } - res, _ := strconv.ParseInt(arr[1], 10, 64) - return res + return getSubVersion(v, 1) } func Minor(v string) int64 { - arr := strings.Split(v, ".") - if len(arr) < 3 { - return 0 - } - res, _ := strconv.ParseInt(arr[2], 10, 64) - return res + return getSubVersion(v, 2) } // add every case there if server will not accept client's protocol and return false