I have test.txt in it 10 different lines. I need to read and write to the array line by line.

Code:

 package main import ( "fmt" "io/ioutil" "strings" ) func main() { bs, err := ioutil.ReadFile("test.txt") if err != nil { return } str := string(bs) fmt.Println(str) strs := strings.Split(str, "\r\n") fmt.Println(strs) // выводится последняя строка файла + ] } 

Conclusion:

 0.0.0.0:80 110.172.167.34:8080 110.5.98.234:3128 110.8.253.100:80 113.6.255.107:80 114.30.47.10:80 114.30.79.62:8080 115.124.67.54:80 116.68.172.35:8080 116.84.187.131:3128 117.102.93.138:80 117.102.95.114:3128 117.35.118.2:3128 118.175.14.108:3128 118.220.175.207:80 118.46.147.201:3128 118.96.102.181:3128 118.96.110.41:3128 118.96.121.70:3128 118.96.148.40:3128 118.96.24.227:8080 118.96.87.107:3128 [0.0.0.0:80 110.172.167.34:8080 110.5.98.234:3128 110.8.253.100:80 113.6.255.107:80 114.30.47.10:80 114.30.79.62:8080 11 5.124.67.54:80 116.68.172.35:8080 116.84.187.131:3128 117.102.93.138:80 117.102.95.114:3128 117.35.118.2:3128 118.175.14 .108:3128 118.220.175.207:80 118.46.147.201:3128 118.96.102.181:3128 118.96.110.41:3128 118.96.121.70:3128 118.96.148.40 :3128 118.96.24.227:8080 118.96.87.107:3128 ] 

Added code to select proxy server:

 str := string(bs) strs := strings.Split(str, "\r\n") bestProxyServer := randInt(len(strs)) fmt.Print(strs) proxyUrl, err := url.Parse(str[bestProxyServer]) httpClient := &http.Client { Transport: &http.Transport { Proxy: http.ProxyURL(proxyUrl) } } response, err := httpClient.Get(host) 

Nothing is selected from the slice, my former IP address remains.

Conclusion:

 [110.5.98.234:3128 110.8.253.100:80 113.6.255.107:80 114.30.47.10:80 114.30.79.62:8080 115.124.67.54:80 116.68.172.35:80 80 116.84.187.131:3128]map[Date:[Mon, 06 Nov 2017 15:59:37 GMT] Content-Type:[text/html; charset=UTF-8] Set-Cookie:[PHPS ESSID=tknp6t8h5m6rmptdb8cvef1so2; path=/ IP%5B1509983977%5D=**92.53.213.181**; expires=Sun, 04-Feb-2018 15:59:37 GMT; Max-Ag e=7776000; path=/; domain=.2ip.ru] Server:[nginx] Connection:[keep-alive] Expires:[Thu, 19 Nov 1981 08:52:00 GMT] Cache- Control:[no-store, no-cache, must-revalidate] Pragma:[no-cache]] 

    1 answer 1

    If you are sure that the file is small, then you just need to divide the line by \n :

     strs := strings.Split(str, "\n") 

    If the file is large, then it is better to use bufio.Scanner . An example is in the documentation.

    • Writes that he cannot use strs as a string in an array: cannot use strs (type [] string) as type string in array or slice literal - Jack Smith
    • @JackSmith You are doing something weird. strs is the slice of lines from your file. Why are you popping them in another slice? - Ainar-G
    • @JackSmith, once again, strs is already a slice. Want to add it to another - use append . - user227465
    • @GreenDragon I already understood, corrected. but only the last line of the file is read to me, 118.86.147.201 I-128 and added] = 118.46.147.20181128] I have not read all 14 lines of the file, only one last line is read. What to do? - Jack Smith