zoukankan      html  css  js  c++  java
  • 62_Go基础_1_29 函数中切片数组参数的区别

     1 package main
     2 
     3 import "fmt"
     4 
     5 func fun2(s2 []int) {
     6     fmt.Println("函数中,切片的数据:", s2) // [1 2 3 4]
     7     s2[0] = 100
     8     fmt.Println("函数中,切片的数据更改后:", s2) // [100 2 3 4]
     9 }
    10 func fun1(arr2 [4]int) {
    11     fmt.Println("函数中,数组的数据:", arr2) // [1 2 3 4]
    12     arr2[0] = 100
    13     fmt.Println("函数中,数组的数据修改后:", arr2) // [100 2 3 4]
    14 }
    15 
    16 func main() {
    17 
    18     /*
    19         数据类型:
    20             一:按照数据类型来分:
    21                     基本数据类型:
    22                         int,float,string,bool
    23                     复合数据类型:
    24                         array,slice,map,struct,interface。。。。
    25 
    26             二:按照数据的存储特点来分:
    27                     值类型的数据:操作的是数值本身。
    28                         int,float64,bool,string,array
    29                     引用类型的数据:操作的是数据的地址
    30                         slice,map,chan
    31 
    32         参数传递:
    33             A:值传递:传递的是数据的副本。修改数据,对于原始的数据没有影响的。
    34                 值类型的数据,默认都是值传递:基础类型,array,struct
    35 
    36             B:引用传递:传递的是数据的地址。导致多个变量指向同一块内存。
    37                 引用类型的数据,默认都是引用传递:slice,map,chan
    38 
    39     */
    40 
    41     arr1 := [4]int{1, 2, 3, 4}
    42     fmt.Println("函数调用前,数组的数据:", arr1) // [1 2 3 4]
    43     fun1(arr1)
    44     fmt.Println("函数调用后,数组的数据:", arr1) // [1 2 3 4]
    45 
    46     fmt.Println("---------------------------------")
    47 
    48     s1 := []int{1, 2, 3, 4}
    49     fmt.Println("函数调用前,切片的数据:", s1) // [1 2 3 4]
    50     fun2(s1)
    51     fmt.Println("函数调用后,切片的数据:", s1) // [100 2 3 4]
    52 }
  • 相关阅读:
    HTTP状态码
    MySQL的order by时区分大小写
    CopyOnWriteArrayList、CopyOnWriteArraySet、ConcurrentHashMap的实现原理和适用场景
    Map接口
    Python中创建守护进程
    df说磁盘空间满了, du说没有,到底谁是对的
    几种分布式文件系统对比
    Unity:控制粒子特效的移动方向
    创建NuGet包
    NuGet的简单使用
  • 原文地址:https://www.cnblogs.com/luwei0915/p/15629678.html
Copyright © 2011-2022 走看看