Golang 基础语法介绍及对比(二)
传值与传参
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 基础语法介绍及对比(二)的更多相关文章
- scala函数式编程(二) scala基础语法介绍
上次我们介绍了函数式编程的好处,并使用scala写了一个小小的例子帮助大家理解,从这里开始我将真正开始介绍scala编程的一些内容. 这里会先重点介绍scala的一些语法.当然,这里是假设你有一些ja ...
- c语言学习之基础知识点介绍(十二):结构体的介绍
一.结构体的介绍 /* 语法: struct 结构体名{ 成员列表; }; 切记切记有分号! 说明:成员列表就是指你要保存哪些类型的数据. 注意:上面的语法只是定义一个新的类型,而这个类型叫做结构体类 ...
- PHP 基础语法介绍
PHP 语法 PHP 脚本在服务器上执行,然后将纯 HTML 结果发送回浏览器. 基本的 PHP 语法 PHP 脚本可以放在文档中的任何位置. PHP 脚本以 <?php 开始,以 ?> ...
- Golang基础语法1
打开cmd命令窗口 保存,编译,执行: 1.保存到一个×××.go的文件(我这里保存到 E:\GoTest\hello.go 下) 2.编译,在命令提示符中执行命令: go build -o E ...
- javaScript基础语法介绍
简介 JavaScript是一种脚本语言. (脚本,一条条的文字命令.执行时由系统的一个解释器,将其一条条的翻译成机器可识别的指令,然后执行.常见的脚本:批处理脚本.T-SQL脚本.VBScript等 ...
- golang基础语法
golang语言的常量定义: const filename="abc.txt"; const filename String="abc.txt" golang ...
- 重新整理 mysql 基础篇————— 介绍mysql日志[二]
前言 对于后端开发来说,打交道最多的应该是数据库了,因为你总得把东西存起来. 或是mongodb或者redis又或是mysql.然后你发现一个问题,就是他们都有日志系统,那么这些日志用来干什么的呢? ...
- 一、vue基础语法(轻松入门vue)
轻松入门vue系列 Vue基础语法 一.HelloWord 二.MVVM设计思想 三.指令 1. v-cloak 2. v-text 3. v-html 4. v-show 4. v-pre 5. v ...
- Golang 基础之基础语法梳理 (二)
大家好,今天将梳理出的 Go语言基础语法内容,分享给大家. 请多多指教,谢谢. 本次<Go语言基础语法内容>共分为三个章节,本文为第二章节 Golang 基础之基础语法梳理 (一) Gol ...
随机推荐
- 树莓派4B 更新wiringPi库到2.52的方法的wiringPi库2.5.2版本wiringpi-latest.deb下载
树莓派4B 更新wiringPi库到2.52的方法 – 树莓派中文站 http://www.52pi.net/archives/1918 通过如上链接可知,需要通过如下命令下载wiringpi-lat ...
- MySQL for OPS 08:MHA 高可用
写在前面的话 主从架构在一般情况下只能满足我们小公司业务并非一刻都不能中断服务.但是对于大型公司而言,对然数据丢失,数据库挂了,我们可以通过技术找回,修复.但是其中修复过程所消耗的时间是不被允许的.此 ...
- mongodb实现文件存储系统
前言:这种坑很深呀,要对应mongodb的版本跟php支持的版本,然后,如果要用composer安装第三方的库,一定要一一对应的 正片开始! 开发环境: 系统:window 开发语言:php+apac ...
- C# 学习笔记 多态(一)虚方法
在面对对象编程中,类的三大特性分别为封装,继承,多态.其中多态的具体实现,依赖于三个方法,也就是虚方法,抽象类和接口. 多态的具体作用是什么呢?或者说多态的存在有什么意义呢?多态的存在有效的降低了程序 ...
- 读 HeadFirst 设计模式
以往虽也看过相关设计模式的书籍,但能和与HeadFirt设计模式这本书相比不是缺乏严谨性就是缺乏具体应用实例,还有幽默生动以及引人启发的观点. 设计原则 设计原则并不能光靠死记硬背,我们需要通过具体的 ...
- uni-app学习心得和填坑,关于uni-app 打包h5 页面的坑
第一次使用博客园写博客 1.我写博客的原因,梳理知识,整理思路,好记性不如烂笔头做个记录吧!记录生活! 1.了解 大概在我使用hbuilder的时候,在官网浏览下载的hbuilder时候无意中发现了u ...
- Violet音乐社区 - 总结报告
目录 一.项目概述 1.1 项目背景 1.2 编写目的 1.3 项目文档 1.4 项目源码 1.5 项目成果 二.个人工作总结 1.1 工作概览 1.1.1 作为项目组组长 1.1.2 作为项目组成员 ...
- 第七篇Scrum冲刺博客
第七篇Scrum冲刺博客 一.站立式会议 提供当天站立式会议照片一张 二.每个人的工作 成员 已完成工作 明天计划完成的工作 遇到的困难 林剑峰 加入搜索页面 无 陆君健 校园卡匹配功能的实现 无 石 ...
- mtime, atime, ctime 的区别
mtime ls -l 显示最近修改文件内容的时间 atime ls -lu 显示最近访问文件的时间 ctime ls -li 显示最近文件有所改变的状态,如文件修改,属性\属主改变,节点,链接变化等 ...
- 学习10:Python重要知识
Python易忽略知识 (1)print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="": (2)isinstance 和 type 的区别在于:type()不 ...