@@ -121,17 +121,20 @@ func ClientFetchHosts(url string) (err error) {
121121 return
122122 }
123123
124+ newlineChar := GetNewlineChar ()
125+
124126 fetchHostsLines := strings .Split (string (fetchHosts ), "\n " )
125127
126- for _ , fetchLine := range fetchHostsLines {
128+ for i , fetchLine := range fetchHostsLines {
127129 line := strings .TrimSpace (fetchLine )
128130 if line == "" || strings .HasPrefix (line , "#" ) {
129131 continue
130132 }
131133 hosts .WriteString (fetchLine )
132- hosts .WriteString ("\n " )
134+ if i != len (fetchHostsLines )- 1 {
135+ hosts .WriteString (newlineChar )
136+ }
133137 }
134-
135138 if err = ioutil .WriteFile (GetSystemHostsPath (), hosts .Bytes (), os .ModeType ); err != nil {
136139 err = ComposeError ("写入hosts文件失败,请用超级管理员身份启动本程序!" , err )
137140 return
@@ -215,15 +218,19 @@ func getCleanGithubHosts() (hosts *bytes.Buffer, err error) {
215218 return
216219 }
217220
221+ newlineChar := GetNewlineChar ()
222+
218223 // clear local hosts github domain
219- localHostsLines := strings .Split (string (hostsBytes ), " \n " )
224+ localHostsLines := strings .Split (string (hostsBytes ), newlineChar )
220225 hosts = & bytes.Buffer {}
221226
222- for _ , localLine := range localHostsLines {
227+ for i , localLine := range localHostsLines {
223228 line := strings .TrimSpace (localLine )
224229 if line == "" || strings .HasPrefix (line , "#" ) {
225230 hosts .WriteString (localLine )
226- hosts .WriteString ("\n " )
231+ if i != len (localHostsLines )- 1 || ! strings .HasSuffix (hosts .String (), newlineChar ) {
232+ hosts .WriteString (newlineChar )
233+ }
227234 continue
228235 }
229236 var clearLine bool
@@ -235,7 +242,7 @@ func getCleanGithubHosts() (hosts *bytes.Buffer, err error) {
235242 }
236243 if ! clearLine {
237244 hosts .WriteString (localLine )
238- hosts .WriteString (" \n " )
245+ hosts .WriteString (newlineChar )
239246 }
240247 }
241248
0 commit comments