Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to use for range loop in Go

4 Answers

0 votes
package main 
    
import ( 
    "fmt"
) 
 
func main() { 
    str := "go java c"
    
    for i, ch := range str {
        fmt.Printf("%#U index %d\n", ch, i)
    }
} 
    
     
     
   
      
/*
run:
       
U+0067 'g' index 0
U+006F 'o' index 1
U+0020 ' ' index 2
U+006A 'j' index 3
U+0061 'a' index 4
U+0076 'v' index 5
U+0061 'a' index 6
U+0020 ' ' index 7
U+0063 'c' index 8
   
*/

 



answered Jun 10, 2023 by avibootz
0 votes
package main 
    
import ( 
    "fmt"
) 
 
func main() { 
    sl := []string{"go", "c", "java"}
    
    for i, val := range sl {
        fmt.Printf("%s : At index %d\n", val, i)
    }
} 
    
     
     
   
      
/*
run:
       
go : At index 0
c : At index 1
java : At index 2
   
*/

 



answered Jun 10, 2023 by avibootz
0 votes
package main 
    
import ( 
    "fmt"
) 
 
func main() { 
    var arr = [5]int{45, 95, 100, 89, 4}
    
    for i, val := range arr {
    	fmt.Printf("index = %d : value = %d\n", i, val)
    }
} 
    
     
     
   
      
/*
run:
       
index = 0 : value = 45
index = 1 : value = 95
index = 2 : value = 100
index = 3 : value = 89
index = 4 : value = 4
   
*/

 



answered Jun 10, 2023 by avibootz
0 votes
package main 
    
import ( 
    "fmt"
) 
 
func main() { 
    mp := map[string]int{
            "one":   5,
            "two":   34,
            "three": 909,
          }
    for key, value := range mp {
        fmt.Println(key, value)
    }
} 
    
     
     
   
      
/*
run:
       
one 5
two 34
three 909
   
*/

 



answered Jun 10, 2023 by avibootz

Related questions

1 answer 103 views
2 answers 234 views
2 answers 226 views
226 views asked Mar 15, 2020 by avibootz
1 answer 83 views
1 answer 186 views
186 views asked Oct 28, 2020 by avibootz
1 answer 172 views
172 views asked Oct 28, 2020 by avibootz
1 answer 181 views
181 views asked Oct 28, 2020 by avibootz
...