Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use range in GO

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use range in GO. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

For of GO language. What can range do?

For of golang... Range is the syntax of go itself and can be used to traverse data structures, which can be traversed with the following data structures

Slice slice

Array array

Map hash table

Channel channel

For... How do I use range?

Let's see how we can use them separately, for... Range is the equivalent of an iterator that traverses the keys / indexes and values of data structures

Array array

Initialize an array

Use for... Range traversal, corresponding to indexes and values

Func main () {myArray: = [5] int {1, 2, 3, 4, 5} for I, v: = range myArray {fmt.Printf ("% d -% d -% p\ n", I, v, & v)}} slice slice

Initialize a slice

Use for... Range traversal, corresponding to indexes and values

MySlice: = [] int {1, 2, 3, 4, 5} for I, v: = range mySlice {fmt.Printf ("% d -% d -% p\ n", I, v, & v)}

Map hash table

Initialize a map hash table

Use for... Range traversal, the key-value pair corresponding to map

MyMap: = map [string] string {"name": "xmt", "hobby": "program", "addr": "mengli",} for k, v: = range myMap {fmt.Printf ("% s -% s -% p\ n", k, v, & v)} channel channel

Create a channel that can buffer 10 int type data

Create a co-program specifically to write data to the channel

The master program traverses the channel and reads the data

Package mainimport "fmt" var myCh = make (chan int, 10) func writeCh () {for I: = 0; I < 5; iTunes + {myCh

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report