Golang 基础语法介绍及对比(二)

传值与传参

Golong

func main() {
     a := 3

     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+1 // 我们改变了a的值
     return *a //返回一个新值
}

func add1(a int) int {
     a = a+1 // 我们改变了a的值
     return a //返回一个新值
}

C#

 static void Main(string[] args)
        {

            int a = 3;

           var r= add1(a);

            Console.WriteLine(r);
            Console.WriteLine(a);

            Console.ReadKey();
        }

    static int add(ref int a )
        {
            a = a + 1;
            return a;
        }
        static int add1(int a)
        {
            a = a + 1;
            return a;
        }

Javascript

Javascript可以用变量作用域这块来比较,函数内部作用域以及全局作用域,这里就不贴代码介绍了

函数申明

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 =1
var a int32  默认就是0
申明的变量都是需要使用的,不然会出现编译错误
var a bool
     fmt.Print(a)  -- false
var a int32
     fmt.Print(a)  0

Golang中提供了多变量申明以及简洁方式声明省略掉了 var以及类型
如:    a:=1   或 多个   a,b:=1,3

C#

int  a;
int a=1;
var a=1;
var bol=true;
int b=3,b1=1;

申明的变量是可以不用的,如果使用就需要对局部变量赋值

Javascript

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

变参

Golang

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

C#

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

Javascript

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

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

 

面向对象 

Golang

type TestObject struct{
   name string
   age int
}

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

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{"黎又铭",22 }
     testb:=TestObjectB{"徐洁敏",12 }
     test.print()
     testb.print()
     
}

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 = 23 };
            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 = 23 };
            var testb = new TestObjectB { name = "徐洁敏", age = 21 };
            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("黎又铭",23);
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("黎又铭",31);
test.print();
var testb=new testObjectB("徐洁敏",31);
testb.print();

在Javascript 用原型链方法来对于Golang中的

猜你喜欢

转载自www.cnblogs.com/liyouming/p/11018471.html