Le ah le ah got golang into the pit series

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The opening of the material, today returned to look at the previous article, easy index a little downward trend. It's not my style, I wonder. A reflection, is this time, the brain assorted chores a little more, things a lot, forget the happiness. The old saying is good: Sorrow also day, music also day, as long as can passable, finished not dead. This really should become a motto, the most time to become a mantra. There is an old saying that: Cry also one day, Smile also day, as long as do not do conscience, is living immortal. Two sentences just make couplet, the new year affixed, categorization malleability than those favorable weather, prosperity and other pairs good, but also practical, you say is not.

Tired from the heart, music from the brain. Everything has its two sides, see what you think. The more you think about it, the better it can be. The more thought the worse, the matter has not become bad, oneself first to frighten oneself a frightened person. So the ancients created a disguise to know not blessed Fable small story, Lu Xun also abstracted out AH q such a plump figure. No conclusion, no growth, who knows what the next second will become. So, joy and joy. Don't make the money of flour, the heart of the white powder.

We talked about the basic usage rules for slices in the last section. Tell the truth, relying on my a little bit of text, can not tell the slice of the thorough, plainly. If you really want to talk about that, you should be asleep if you don't wait to finish the job. Get started, it's enough. After entering the door, really need to use so high-level rules, I have to check the time. So don't spray speak shallow, speak deep is not my style.

In this section, let's go on to the slice (array) Feeling and talk about how to iterate through the iterations. When I first approached Golang, I often needed to iterate over the elements in the algebraic group, and the simplest way was to write a subscript, and then keep accumulating and removing the value of the underlying. This is a common way to go, but what if you encounter a map? It doesn't apply. So Golang built in a range operation to iterate over the collection data structure.

First look at how to iterate over an algebraic group or slice:

   numbers := []int{0,1,2,3,4,5,6,7,8}    for i:= range numbers {      fmt.Println("Slice item",i,"is",numbers[i])   }

The range is followed by a slice variable, and the front is definitely for. At this point I is the array subscript, cycle once, I will increment one, until the numbers cycle is complete. It says that besides the ability to iterate over slices, you can also map. Because map has key and value, range has two ways of writing it:

   countryCapitalMap := map[string] string {"France":"Paris","Italy":"Rome","Japan":"Tokyo"}   for country := range countryCapitalMap {      fmt.Println("Capital of",country,"is",countryCapitalMap[country])   }   for country,capital := range countryCapitalMap {      fmt.Println("Capital of",country,"is",capital)   }

At this point, range can take out key in turn, or you can remove both key and value. It is important to note that key is not guaranteed by Ragne, but is determined by map. The range operation is simply to fetch the elements in the data collection in turn.

Originally range operation is need to put in the last section said, but near noon to eat, so do not write lazy. This section complements, anyway the range operation is just that. But since we mentioned map, let's talk about map. Usually I also like this, casually to a conversation can continue to chat down, a few friends nonsense, not just a job, no words to find words. Prior to the draft, paramilitary is premeditated good (here to sincerely praise the company's CxO, casually a person voluminous can talk show for a few hours, want to melt into the capital, must play small to learn to have organized the nonsense light). So write where you want, and talk about map below.

Map is basically a necessary data type in various programming languages, so Golang can't pull this important thing. Golang no more than Java,map is the interface, but also realizes the Hashmap,linkedhashmap and TreeMap. There are not so many implementations in Golang, only HashMap. Not to say a map, that is bitmaps (there should be no other map, view official documents and view Golang source code also did not find other maps.) If you find another type of map, please let me know, it should be gone.

Bitmap and HashMap are not all people, so don't mention it. So the map below refers to HashMap. First say how to create a map, here to use the Make function, in the creation of slices used it, in the Golang to create a variety of data types, basic will use it.

map_variable := make(map[key_data_type]value_data_type)

For example, we create a map,key that is string,value is int. Is

myMap := make(map[string]int)

You do not need to specify the size because map will automatically expand. This allows you to use the mymap below. If you like the style of genteel, you can also do this:

var myMap map[string]intmyMap = make(map[string]int)

The effect is the same, but the number of lines of code becomes two lines (V2ex there are some ironies that if my article is counted as a fortune.) Sorry, there is no charge for this. And which company does it count as a row? The typical speech but the brain, the irony can not find the acupoints). Or that sentence, choose whichever is OK, happy is good.

Combining the range above, the map also blends in:

package mainimport "fmt"func main() {   var countryCapitalMap map[string]string   countryCapitalMap = make(map[string]string)   countryCapitalMap["France"] = "Paris"   countryCapitalMap["Italy"] = "Rome"   countryCapitalMap["Japan"] = "Tokyo"   countryCapitalMap["India"] = "New Delhi"   for country := range countryCapitalMap {      fmt.Println("Capital of",country,"is",countryCapitalMap[country])   }}

In the daily use process, there are two operations often appear, one determines whether the specified key exists. Two deletes the specified key.

First answer the first question, if you determine whether the specified key exists:

   capital, ok := countryCapitalMap["United States"]   if(ok){      fmt.Println("Capital of United States is", capital)     } else {      fmt.Println("Capital of United States is not present")    }

OK is a bool value that indicates whether the specified key exists and is straightforward. Then answer the second question, how do I delete it?

delete(countryCapitalMap,"France")

Well, just use the delete function, simple and rude. For Golang, personal feeling should be a relatively low entry difficulty of a language, in addition to the grammar slightly to adapt to a little, there is not much study cost. Previous knowledge and reserves do not need to be thrown away, change a shell can be used.

Today Friday (12.1), I do not know how much you can see, anyway, I do not write much. So two fingers around the head, rest, rest a little bit.

It is said that the e-mail address ztao8607@gmail.com Here, will receive many sister's mail. Believe it or not, I believe it anyway. :)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.