go语言学习-go语言中的方法

量化人必看|miniQMT踩坑记:回测太慢?一招教你把行情数据“搬回家”! 这篇文章主要聊的是。 阅读详情

方法

首先强调一下:go中任何自定义类型都可以有方法,不仅仅是struct
注意除了:指针和interface

通过下面简单例子理解:

 

package main

import (
    "fmt"
)

//这里是我们普通定义的一个函数add
func add(a,b int) int {
    return a+b
}

type Int int

//这里是对Int这个自定义类型定义了一个方法add
func (i Int) add(a,b int) int{
    return a+b
}
//如果想要把计算的结果赋值给i
func(j *Int) add2(a,b int){
    *j = Int(a+b)
    return
}

func main(){
    c := add(100,200)
    fmt.Println(c)
    var b Int
    res := b.add(10,100)
    fmt.Println(res)

    var sum Int
    sum.add2(20,20)
    fmt.Println(sum)

}

 

方法的定义:

func(receiver type)methodName(参数列表)(返回值列表){

}

下面是给一个结构体struct定义一个方法

 

package main

import (
    "fmt"
)


type Student struct{
    Name string
    Age int
}

func (stu *Student)Set(name string,age int){
    stu.Name = name
    stu.Age = age
}

func main(){
    var s Student
    s.Set("tome",23)
    fmt.Println(s)
}

 

注意:方法的访问控制也是通过大小写控制的

在上面这个例子中需要注意一个地方func (stu *Student)Set(name string,age int)这里使用的是(stu *Student)而不是(stu Student)这里其实是基于指针对象的方法

二元交叉熵:分类模型概率校准的数学根基 二元交叉熵(BCE)是二分类任务中实现概率校准的核心损失函数,其本质源于最大似然估计与KL散度,确保模型输出具备真实可解释的概率意义。相比均方误差(MSE)或Hinge Loss,BCE强制优化预测分布与真实伯努利分布之间的信息差异,使p=0.8真正对应约80%的正例发生率,支撑风控、医疗、推荐等高信度场景的阈值决策。它不只影响训练收敛,更决定部署后概率的业务可信度——这正是Focal Loss等变体无法替代其基础地位的根本原因。理解BCE,就是理解分类模型如何从‘打分器’进化为‘概率契约签署者’。 阅读详情

相关推荐

Inside NAND Flash Memories.中文版. Chatper 2 [2.1-2.2.1]

2.1 Introduction 半导体存储器分类: RAM:Random Access Memories--掉电丢失 ROM:Read Only Memories--只读,出厂时写好;掉电不丢失;不可更改。 NVM:Non-Volatile Memories--电可擦除/写入,并且掉电不丢失。第一类产品为EPROM memory (Erasable Programmable Read...

xuqikang的专栏 3617

Golang 基础之数据类型梳理

大家好,将梳理出的 Go语言数据类型内容,分享给大家。 请多多指教,谢谢。

changhao 861

VMFS数据存储(datastore)丢失后恢复步骤

vmfs datastore恢复步骤

jetliu05的博客 3278

Golang - 变量和数据类型的声明

Golang 变量及基本数据类型的声明

业精于勤荒于嬉 1174

Lammps实现随机粗糙度表面沸腾

Lammps实现随机粗糙度表面沸腾前言 前言

Rockyoung 2706

Android11车机系统海外项目资源包RRO方案优化

本文介绍了汽车设置应用如何通过Android RRO(运行时资源叠加)方案实现多车型差异化资源管理。针对不同车型(国内/海外、轿车/SUV)和配置(方向盘、轮毂等),采用产品变体(productFlavors)构建不同APK版本,并通过JSON配置文件动态启用对应资源包。系统根据车辆下线配置自动匹配适用的资源叠加包,解决了硬编码判断带来的维护问题。该方案实现了资源的高效复用,同时支持灵活配置多种车型组合,为汽车行业多车型共平台开发提供了可扩展的技术实现。

shusuanly的博客 475

Go学习(二) 变量定义和初始化

go学习,变量的定义以及初始化

smallbit1989的博客 446

LFM线性调频信号的MATLAB仿真

线性调频(LFM)是一种不需要伪随机编码序列的扩展频谱调制技术。由于线性调频信号占用的频带宽度远大于信息带宽,所以也可以获得很大的系统处理增益。线性调频信号又称鸟声(Chirp)信号,因为其频谱带宽落于可听范围,则听若鸟声,所以又称Chirp扩展频谱(CSS)技术。LFM技术在雷达、声纳技术中有广泛应用,如在雷达定位技术中,它可在增大射频脉冲宽度、提高平均发射功率、加大通信距离同时又保持足够的信号频谱宽度,不降低雷达的距离分辨率。线性调频(LFM)是一种不需要伪随机编码序列的扩展频谱调制技术。

平时工作较为繁忙,私信消息一般晚上回复,谢谢大家~~ 907

Golang 方法定义与使用

方法 有些函数是专门给特定的结构体对象使用的,比如对包外不可见的结构体调用可见方法来修改里面属性和获取里面属性,既然是对某个特定结构体的函数,那么有没有这种函数供特定结构体来调用呢?也就是让函数的作用域是针对某种结构体。 所以这个时候就有了方法,方法是为特定类型定义的,只能由该类型调用的函数。 1. 定义 方法是添加了接收者的函数,接收者必须是自定义的类型(方法就是在函数名称之前加了一个接收者,接受者就是调用者,只能由谁来调用) type privateStruct struct

小楼一夜听春雨,深巷明朝卖杏花 2170

【CANoe示例分析】0002_SOMEIPDemo

该工程由Vector官方提供,作为仿真SOME/IP节点的示例。Demo中介绍了两种仿真SOME/IP节点的方法,一种是基于arxml数据库的仿真,另一种是没有数据库(arxml、fibex)的仿真。无论是哪种形式的仿真,如果想要通过CAPL程序接收或者发送SOME/IP信息,都需要添加交互层的信息,这些信息可以通过添加DLL文件实现,DLL文件路径在CANoe安装目录下的Exec32文件中。

jasonj333 3524

学Simulink--基于新能源与储能系统的场景实例:压缩空气储能(CAES)系统热力学建模

本文介绍了使用Simulink对压缩空气储能(CAES)系统进行热力学建模的详细流程。重点讲解了系统各模块的设计方法,包括压缩机、储气装置、膨胀机和热交换器等核心组件的参数设置与计算模型。文章还提供了控制策略设计和仿真运行的具体步骤,通过建立MATLAB函数计算关键参数变化,并分析了系统性能评估指标。该建模方法可用于验证CAES系统的设计有效性,并为优化储能效率提供技术参考。全文采用模块化设计思路,为新能源与储能系统的仿真研究提供了实用案例。

xiaoheshang_123的博客 301

GO语言入门详解(五)之 方法

这一点上,Go和很多其它的面向对象的语言不太一样。因此在Go语言里,我们为一些简单的。

majunssz的博客 1092

【华为OD技术面试手撕真题】134、爬楼梯 | 手撕真题+思路参考+代码解析(C & C++ & Java & Python & JS)

【华为OD技术面试手撕真题】134、爬楼梯 | 手撕真题+思路参考+代码解析(C & C++ & Java & Python & JS) 华为od机试,华为od机试真题,华为od机试E卷,华为odE卷,华为od机试真题E卷,华为OD,华为OD机试,华为OD机试真题,华为od机考,华为od机试D卷,华为od机试真题D卷,华为od手撕代码,华为od,华为od机试C卷,华为od e卷,华为2025e卷,od机试,2025E卷,华为OD机试E卷,什么是华为od,华为od介绍,全面了解华为od,2025华为od,如何

KJ.JK 2187

Golang Context 的使用指南_golang context 使用(1),2024年最新面试Golang系统架构

Context 是 Golang 中处理并发和请求的重要机制之一。通过使用 Context,我们可以有效地控制和管理 goroutine 的生命周期,并实现超时控制和取消操作。使用 Context 可以避免因一个请求出现问题而导致其他请求也出现问题的情况。同时,Context 的传播机制能够自动取消所有依赖于父 Context 的子 Context,保证所有相关的操作能够正确地停止。通过本文的介绍,您应该已经了解了如何使用 Context 来管理和控制 Golang 中的并发操作。

2401_84247849的博客 679

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【七天入门Go语言】 文件 &amp;&amp; 包 第五天_go xml directive

升的进阶课程,基本涵盖了95%以上Golang知识点,真正体系化!**

acupofcoffee的博客 833

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

2024年Go最全Golang 继承,2024年最新最新Golang面试题整理

【代码】2024年Go最全Golang 继承,2024年最新最新Golang面试题整理。

2401_84911346的博客 442

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Go最全Golang 并发编程_go 语言取消并行(2),2024年最新一个月成功收割腾讯、阿里、字节offer

c := make(chan string) // 创建一个通道 channel, 无缓冲区, 使用时要同时具备输入和输出, 即该管道要同时可写入和读取。go foo() // 新建新的 goroutine 去执行函数 foo。foo() // 执行函数 foo, 程序等待函数 foo 返回。bar() // 不用等待函数 foo 返回, 两个函数并发执行。// Foo 定义结构体, 相当于类。// Foo 定义结构体, 相当于类。// 接收者, 方法。// 接收者, 方法。

2401_84904706的博客 783

go语言学习笔记 — 接口 — 实现接口的条件(2):接口中的方法都被实现

当一个接口含有多个方法时,只有这些接口方法都被实现了,我们才能正确编译并使用接口。如下所示,在接口中包含WriteData方法和CanWrite方法,只有在file结构体绑定方法WriteData和CanWrite之后,才能正常使用接口。 type DataWwriter interface { WriteData(data interface{}) CanWrite()...

yjk13703623757的博客 488

Go/golang语言学习笔记(二):变量、数据类型、指针、标识符

go语言学习笔记二

qq_63536895的博客 456

go语言学习笔记 — 进阶 — 接口(2.2):实现接口的条件 —— 接口中的方法都被实现

当一个接口含有多个方法时,只有这些接口方法都被实现了,我们才能正确编译并使用接口。如下所示,在接口中包含WriteData方法和CanWrite方法,只有在file结构体绑定方法WriteData和CanWrite之后,才能正常使用接口。 type DataWwriter interface { WriteData(data interface{}) CanWrite() bool // 新增接口方法CanWrite } type file struct{} func (d *

yjk13703623757的博客 220

Go语言方法和接收器、为任意类型添加方法_go 任意类型,2024年最新一线互联网架构师筑基必备技能之Golang篇

方法就是作用在接收器(receiver)上的一个函数;某个类型的变量;为什么有函数了还需要方法呢?Go不是纯粹的面向对象编程语言,而且Go不支持类。因此基于类型的方法是一种实现和类相似行为的途径相同的名字的方法可以定义在不同的类型上,而相同名字的函数是不被允许的。在Go语言中,结构体就像是其他语言中类的一种简化形式,那么类的方法在哪里呢?

2401_84267675的博客 1907

Go语言学习

@TOP

qq122516902的博客 407

Go语言学习笔记—golang方法

Go中的方法,是一种**特殊的函数**,定义于结构体struct之上(与struct关联、绑定),被称为struct的接受者(receiver)。通俗的讲,方法就是有接收者的函数。

qq_39280718的博客 485

用于科学计算的Go语言教程 | Gopher Daily (2021.08.02) ʕ◔ϖ◔ʔ

每日一谚:In Go, constants are mathematically precise.Go技术生态Go是如何计算len()的?- https://tpaschalis.gith...

TonyBai 723

Go语言学习[1]_环境配置及语言结构

Go语言学习 道法自然 文章目录Go语言学习前言一、windows下配置go环境1.1下载go1.2下载vscode二、语言结构-实现“hello world”1.语言结构2.测试结果总结 前言 Go(又称Golang)是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。Go语言语法简单,包含了类C语法。快速的编译时间,开发效率和运行效率高。组合的思想、无侵入式的接口。 一、windows下配置go环境 1.1下载go 下载地址: 链接: https://stu

牛博通的博客 239

go get 拉取指定版本_go语言学习笔记-基础知识-3

相关文档go语言学习笔记-目录1、简介1.1 什么是GOGo 是一个开源的编程语言,它能让构造简单、可靠且高效的软件变得容易。Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发,后来还加入了Ian Lance Taylor, Russ Cox等人,并最终于2009年11月开源,在2012年早些时候发布了Go 1稳定版本。现在Go的开发已经...

weixin_33508411的博客 1434
上一篇: Go语言学习-接口
下一篇: Go语言学习-marshal unmarshal
SpringBears
博客等级 码龄9年 2粉丝 73原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址