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 ...
随机推荐
- springboot学习源码
springbootTest 学习源码链接 启动前,需要创建数据库表,修改自己的链接配置 create database test01; use test01; CREATE TABLE catego ...
- Spring Cloud Eureka 服务注册中心(二)
序言 Eureka 是 Netflix 开发的,一个基于 REST 服务的,服务注册与发现的组件 它主要包括两个组件:Eureka Server 和 Eureka Client Eureka Clie ...
- 2019-11-29-WPF-元素裁剪-Clip-属性
原文:2019-11-29-WPF-元素裁剪-Clip-属性 title author date CreateTime categories WPF 元素裁剪 Clip 属性 lindexi 2019 ...
- 你不知道的Golang map
在开发过程中,map是必不可少的数据结构,在Golang中,使用map或多或少会遇到与其他语言不一样的体验,比如访问不存在的元素会返回其类型的空值.map的大小究竟是多少,为什么会报"can ...
- TCP SYN flood洪水攻击原理和防御破解
简介 TCP协议要经过三次握手才能建立连接: 于是出现了对于握手过程进行的攻击.攻击者发送大量的SYN包,服务器回应(SYN+ACK)包,但是攻击者不回应ACK包,这样的话,服务器不知道(SYN+AC ...
- OpenJDK下SpringBoot使用HttpSession时页面打开卡住
近期将一个老项目向ARM版的CentOS7移植时,遇到了SpringBoot启动顺利,但访问页面卡住的问题.由于是aarch64架构,因此使用了openjdk,这个项目之前在x86_64环境下一直是用 ...
- Java生鲜电商平台-RBAC系统权限的设计与架构
Java生鲜电商平台-RBAC系统权限的设计与架构 说明:根据上面的需求描述以及对需求的分析,我们得知通常的一个中小型系统对于权限系统所需实现的功能以及非功能性的需求,在下面我们将根据需求从技术角度上 ...
- MySQL基础(一)(启动/停止、登录/退出、语法规范及最基础操作)
1.启动/停止MySQL服务 启动:net start mysql 停止:net stop mysql 2.MySQL登录/退出 登录:mysql 参数:如果连接的是本地服务器,一般用命令:my ...
- 记一次CTF出题WP
笔者有幸参与一次CTF入门级的出题,在此记录一下WP 1.测试你得手速 1.用IDA打开程序 找到单击次数, 获取全局变量地址. 打开程序 打开OllyDbg attcach后在左下角按CTRL+N ...
- php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos
php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpossubstr_count($haystack, $needle [,$o ...