मैं गो भाषा में नया हूं। मैंने खुद को array
और slice
डेटा प्रकारों से भ्रमित पाया। एक सरणी के रूप मेंगो लैंग सरणी और स्लाइस डेटा प्रकार
As in all languages in the C family, everything in Go is passed by value. That is, a function always gets a copy of the thing being passed, as if there were an assignment statement assigning the value to the parameter. For instance, passing an int value to a function makes a copy of the int, and passing a pointer value makes a copy of the pointer, but not the data it points to.
क्यों sort.Ints(arrayValue)
पारित कर दिया चर जब मैं यह घोषित संशोधित करता है, नहीं एक टुकड़ा के रूप में:
There are major differences between the ways arrays work in Go and C. In Go,
- Arrays are values. Assigning one array to another copies all the elements.
- In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it.
- The size of an array is part of its type. The types [10]int and [20]int are distinct.
कार्य:
जाओ डॉक्स से, सरणियों का वर्णन कर रहे हैं?
कोड
var av = []int{1,5,2,3,7}
fmt.Println(av)
sort.Ints(av)
fmt.Println(av)
return
आउटपुट
[1 5 2 3 7]
[1 2 3 5 7]
इसके अतिरिक्त, sort.Ints आपको एक त्रुटि देगा यदि आपने इसे पास किया है एक टुकड़ा के बजाय सरणी। Sort.Ints के लिए प्रलेखन एक टुकड़ा निर्दिष्ट करता है। https://golang.org/pkg/sort/#Ints – Kirk
@ किर्क अच्छा बिंदु। मैंने इसे अधिक दृश्यता के उत्तर में शामिल किया है। – VonC