传值与传参

Golong

func main() {
a := fmt.Println("a = ", a) // 应该输出 "a= 3" a1 := add1(a) //调用add1(x) fmt.Println("a+1 = ", a1) // 应该输出"a+1 = 4"
fmt.Println("a = ", a) // 应该输出"a = 3"
} func add(a *int) int {
*a = *a+ // 我们改变了a的值
return *a //返回一个新值
} func add1(a int) int {
a = a+ // 我们改变了a的值
return a //返回一个新值
}

C#

 static void Main(string[] args)
{ int a = ; var r= add1(a); Console.WriteLine(r);
Console.WriteLine(a); Console.ReadKey();
} static int add(ref int a )
{
a = a + ;
return a;
}
static int add1(int a)
{
a = a + ;
return a;
}

Javascript

Javascript可以用变量作用域这块来比较,函数内部作用域以及全局作用域

 var a=;
function main()
{
var r=add(a);
alert("r:"+r+"\r\n"+"a:"+a);
} function add(a){
a= a+;
return a;
}
 var a=;
function main()
{
var r=add(a);
alert("r:"+r+"\r\n"+"a:"+a);
} function add(x){
a= x+;
return a;
}

这里还有一个有不同的地方就是C#与Golang在Class、Struct

在C#中Class就是引用类型,C#中的Struct也是值类型的,Golang中的Struct就类似C#中的Struct,下面来看下这几种输出结果

C# Class

所以这里给不给ref都是一样的结果,都会变成引用类型

static void Main(string[] args)
{
var test = new TestObject { name = "黎又铭", age = };
var t= Con(test);
// var t1 = Con(ref test);
Console.WriteLine(t.name);
Console.WriteLine(test.name);
Console.ReadKey();
}
static TestObject Con(TestObject testObject)
{ testObject.name = "张三";
return testObject; } static TestObject Con(ref TestObject testObject)
{
testObject.name = "张三";
return testObject; }

C# Struct

用Struct来输入就发现其实用ref 与不用输出的结果就不一样了

public struct TestObjectB
{
public string name { get; set; }
public int age { get; set; }
}
static TestObjectB Con(TestObjectB testObject)
{ testObject.name = "张三";
return testObject; } static TestObjectB Con(ref TestObjectB testObject)
{
testObject.name = "张三";
return testObject; } static void Main(string[] args)
{
var test = new TestObjectB { name = "黎又铭", age = }; var t= Con(test);
// var t1 = Con(ref test); Console.WriteLine(t.name);
Console.WriteLine(test.name); Console.ReadKey();
}

Golang Struct

这里我们来看看Golang中的写法,在使用TestZZ得到的结果是不同的

func main() {

     test:=TestObject{"黎又铭", }

     t:= TestZZ1(test)
// t:= TestZZ(&test)
f.Println(t.name)
f.Println(test.name) }
func TestZZ(test *TestObject) *TestObject { test.name="zhangsan"
return test
}
func TestZZ1(test TestObject) TestObject{ test.name="zhangsan"
return test
}

函数申明

Golang

func testMethod(arg1 int) int {

方法体
}
//解析:
方法关键字 方法名称(参数1 参数1类型) 返回值
{
方法体
} Golang 返回参数可以是多个

C#

public int testMethod(int arg1)
{ } //解析:
访问关键字 返回值 方法名称(参数1类型 参数1)
{
方法体
} C#返回参数只能是一个,如果是多个需要用对象类定义

Javascript

function testMethod(arg1){

方法体

}
//解析:
可以返回任意类型,弱类型语言,当然也可以返回 function ,{} ,[]

变量声明

Golong

var a int32 =
var a int32 默认就是0
申明的变量都是需要使用的,不然会出现编译错误
var a bool
fmt.Print(a) -- false
var a int32
fmt.Print(a) Golang中提供了多变量申明以及简洁方式声明省略掉了 var以及类型
如: a:= 或 多个 a,b:=,

C#

int  a;
int a=;
var a=;
var bol=true;
int b=,b1=; 申明的变量是可以不用的,如果使用就需要对局部变量赋值

Javascript

var a=;
var a=,b=;
var a;
javascript也是可以申明多个变量的

变参

Golang

Golang中用了 ...int 这样的形式
func main() {
sum:= Sum(,,,)
fmt.Println(sum) //
}
func Sum(arg1 ...int) int {
sum:=
for _,x := range arg1 {
sum+=x
}
return sum
}

C#

这里参数需要使用关键字 params 如下
static int Sum(params int[] a)
{
int sum = ;
foreach(var item in a)
{
sum += item;
}
return sum;
}
static void Main(string[] args)
{
var r= Sum(, , , );
Console.WriteLine(r); //
Console.ReadKey();
}

Javascript

Javascript变参可以用arguments与 apply和call 来理解,但是这里最趋近于apply,因为call还是要给个数,而apply给的是数组

function Sum()
{
var sum=;
for(var i=;i<arguments.length;i++)
{
sum+=arguments[i];
}
return sum;
}
alert(Sum.apply(this,[,,,]));

面向对象

Golang

type TestObject struct{
name string
age int
} func main() {
test:=TestObject{name:"黎又铭",age: }
f.Println("姓名", test.name)
}
golang在这里也可以这样写:
person:=TestObject{"黎又铭", } 不用写名称,类似构造函数,但是需要注意顺序问题

type TestObject struct{
name string
age int } type TestObjectB struct{
name string
age int } func (test TestObject) print() {
f.Println(test.name) }
func (testb TestObjectB) print() {
f.Println( testb.name) } func main() { test:=TestObject{"黎又铭", }
testb:=TestObjectB{"徐洁敏", }
test.print()
testb.print() }

分别在 TestOjbect 或 TestObjectB 下添加的方法 print,而

func (test TestObject) print()

(test TestObject) 这种语法指定了调用者,C#中就像是在类中添加方法一样

C#

 public class TestObject{
public string name { get; set; }
public int age { get; set; }
}
static void Main(string[] args)
{
var test = new TestObject { name = "黎又铭", age = };
Console.WriteLine("姓名:" + test.name);
Console.ReadKey();
}

   public class TestObject{
public string name { get; set; }
public int age { get; set; }
}
public class TestObjectB
{
public string name { get; set; }
public int age { get; set; }
}
public static class Extensions {
public static string print(this TestObject test)
{
return test.name;
}
public static string print(this TestObjectB testb)
{
return testb.name;
}
}
static void Main(string[] args)
{
var test = new TestObject { name = "黎又铭", age = };
var testb = new TestObjectB { name = "徐洁敏", age = };
var t = test.print();
var tb = testb.print();
Console.WriteLine(t);
Console.WriteLine(tb);
Console.ReadKey();
} 修正:可能用扩展来表达不合理,这里就是在类中添加方法,表示这个类中的方法,这里我们用了扩展不合理

Javascript

function testObject(n,a)
{
this.name=n;
this.age=a; }
var test=new testObject("黎又铭",);
alert(test.name);

function testObject(n,a)
{
this.name=n;
this.age=a; }
function testObjectB(n,a)
{
this.name=n;
this.age=a; }
testObject.prototype.print=function(){
alert(this.name);
}
testObjectB.prototype.print=function(){
alert(this.name);
}
var test=new testObject("黎又铭",);
test.print();
var testb=new testObjectB("徐洁敏",);
testb.print(); 在Javascript 用原型链方法来对于Golang中的

C# 函数作为参数传递(委托)

delegate void TestMethod(int x);

        public static void mytest(int a)
{
Console.WriteLine(a);
}
public static void mytest1(int a)
{
Console.WriteLine("ddd"+a);
}
static void Main(string[] args)
{
TestMethod testMethod= new TestMethod(mytest);
testMethod += new TestMethod(mytest1);
testMethod?.Invoke();
}

Golang 函数作为值、类型

type testMethod func(int) int
func testprint(a int) int{
f.Println(a)
return a*a;
}
func testprint1(a int) int{ f.Println(a)
return a+a;
}
func test(t testMethod) int{
x:= t()
return x
}
func main() {
x:= test(testprint)
x1:= test(testprint1)
f.Println(x)
f.Println(x1)
}

Golang 基础语法介绍及对比(二)的更多相关文章

  1. scala函数式编程(二) scala基础语法介绍

    上次我们介绍了函数式编程的好处,并使用scala写了一个小小的例子帮助大家理解,从这里开始我将真正开始介绍scala编程的一些内容. 这里会先重点介绍scala的一些语法.当然,这里是假设你有一些ja ...

  2. c语言学习之基础知识点介绍(十二):结构体的介绍

    一.结构体的介绍 /* 语法: struct 结构体名{ 成员列表; }; 切记切记有分号! 说明:成员列表就是指你要保存哪些类型的数据. 注意:上面的语法只是定义一个新的类型,而这个类型叫做结构体类 ...

  3. PHP 基础语法介绍

    PHP 语法 PHP 脚本在服务器上执行,然后将纯 HTML 结果发送回浏览器. 基本的 PHP 语法 PHP 脚本可以放在文档中的任何位置. PHP 脚本以 <?php 开始,以 ?>  ...

  4. Golang基础语法1

    打开cmd命令窗口 保存,编译,执行: 1.保存到一个×××.go的文件(我这里保存到  E:\GoTest\hello.go   下) 2.编译,在命令提示符中执行命令: go build -o E ...

  5. javaScript基础语法介绍

    简介 JavaScript是一种脚本语言. (脚本,一条条的文字命令.执行时由系统的一个解释器,将其一条条的翻译成机器可识别的指令,然后执行.常见的脚本:批处理脚本.T-SQL脚本.VBScript等 ...

  6. golang基础语法

    golang语言的常量定义: const  filename="abc.txt"; const filename String="abc.txt" golang ...

  7. 重新整理 mysql 基础篇————— 介绍mysql日志[二]

    前言 对于后端开发来说,打交道最多的应该是数据库了,因为你总得把东西存起来. 或是mongodb或者redis又或是mysql.然后你发现一个问题,就是他们都有日志系统,那么这些日志用来干什么的呢? ...

  8. 一、vue基础语法(轻松入门vue)

    轻松入门vue系列 Vue基础语法 一.HelloWord 二.MVVM设计思想 三.指令 1. v-cloak 2. v-text 3. v-html 4. v-show 4. v-pre 5. v ...

  9. Golang 基础之基础语法梳理 (二)

    大家好,今天将梳理出的 Go语言基础语法内容,分享给大家. 请多多指教,谢谢. 本次<Go语言基础语法内容>共分为三个章节,本文为第二章节 Golang 基础之基础语法梳理 (一) Gol ...

随机推荐

  1. Autoware 笔记 No. 5——基于GNSS的定位

    1. 前言 在之前的笔记No.2 中,我们直接采用ndt_matching的方法实现定位,但需要在打开rviz中,通过2D Pose Estimate指定初始位置.加入GNSS后,可以帮助ndt_ma ...

  2. 个性化你的 Git Log 的输出格式

    个性化你的 Git Log 的输出格式

  3. Python - File - 第十八天

    Python File(文件) 方法 open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OS ...

  4. SQL server已经设置为单用户模式,Sql server还原失败数据库正在使用,无法获得对数据库的独占访问权

    如果已经设置为单用户模式,还是报这个错误的话,就按照一下SQL执行就

  5. angular获取时间

    获取24小时制的时间<h1> {{time}} </h1> <script> angular.module('app', []) .controller('ctrl ...

  6. 点击除指定区域外的空白处,隐藏div

    <script> $(document).click(function (e) { var $target = $(e.target); //点击.zanpl和.quanzipl以外的地方 ...

  7. 8 Best DDoS Attack Tools (Free DDoS Tool Of The Year 2019)

    #1) HULK Description: HULK stands for HTTP Unbearable Load King. It is a DoS attack tool for the web ...

  8. SQLMAP 速查手册

    /pentest/database/sqlmap/txt/ common-columns.txt 字段字典 common-outputs.txt common-tables.txt 表字典 keywo ...

  9. ssh关闭服务关闭 nohup

    默认输出 # nohup cmd & 指定输出路径 # nohup cmd > mylog.out 2>&1 &

  10. JS 判断设备来源

    1.js代码判断当前设备: function deviceType(){ var ua = navigator.userAgent; var agent = ["Android", ...