- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
最近开始使用Golang,遇到了一个问题:
我有两个结构体,human
和 alien
,它们都基于 creature
结构体。我想根据 if 语句中的 isAlien
bool 值初始化其中之一。
使用 human := human{}
表示法或 if block 内的外来等效符号进行初始化,无法从 if 语句外部访问实例。
另一方面,在 if 语句之前声明变量的类型和名称并在 if 语句中初始化变量的通常解决方案不起作用,因为有两种不同的类型:
var h human //use human or alien here?
if isAlien {
h = alien{} //Error: incompatible types
} else {
h = human{}
}
//same when swapping human with alien at the declaration
我知道我可以在 if 语句之前声明这两种类型,但这个解决方案对我来说似乎并不优雅。
我在这里缺少一些明显的解决方案吗?
最佳答案
如您所述,此语句清楚地表明了问题:
var h human //use human or alien here?
如果您计划在创建对象后在那里使用那个 h
变量,那么 h
的类型必须是可以接受 human
或 alien
作为值。
在 Go 中实现这一点的方法是使用一个 ìnterface
,alien
和 human
都可以实现。
所以你应该像这样声明一个接口(interface):
type subject interface {
// you should list all functions that you plan to use on "h" afterwards
// both "human" and "alien" must implement those functions
}
然后:
var h subject
会成功的。
关于go - 使用类型取决于 bool 值的全局范围初始化结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50546218/
在具有资源的Grails 2.3X的旧世界中,可以按特定顺序定义实际资源,即dependsOn 我一直在寻找 Assets 说明: https://github.com/bertramdev/asse
嗨,我正处于一个场景中,我正在考虑拥有多个模块,并让模块依赖于其他模块。 假设我们正在构建一个业务系统,其中一部分是进销存,并且我们在不同的分支机构都有客户。 因此,一个行业可能希望使用某些工具并以自
#include int main(void) { char ch; int count; while((ch=getchar())!=EOF){ if(ch
我想使用基于 gcc 开关的 #if 指令交叉编译特定的 32 位和 64 位代码。 我可以使用 GCC 设置的任何宏来代替使用 -D 开关设置宏吗? 我能否以某种方式获取 gcc 命令选项中使用的开
这是对我之前的 question 的更复杂的后续操作.答案是使用矩阵,但这不适用于具有不同模式值的数据框。 我想将不同大小的数据框与字符和整数列组合在一起,并根据多个条件计算它们的总和。 状况 仅针对
希望获得有关 dependsOn 的一些指导和解释。 我在模板中看到有两种在模板中提供依赖项的方法。 一种方法是提供resourceId,另一种方法是使用concat提供字符串值。我试图理解两者之间的
我编写了一些包含一些额外函数的包,用于处理反射(reflect)我喜欢使用它们的方式的常见类型(例如,我更喜欢将 Bson 文档视为 map ,而不是字段列表)。我将这些包保存在本地源目录中,而不是在
我有类似于这样的 View 模型: public class MyDialogViewModel { public MyButtonViewModel[] Buttons{ get; }
我只使用一项 Activity ,很少使用碎片。我只想在启动画面上隐藏工具栏。我这样写: class MainActivity : AppCompatActivity() { lateinit var
在我的 M-V-VM 应用程序中,我必须显示用户的头像。图像在 ViewModel 对象的 ImageSource 类型的属性中提供。这就是我目前拥有的: 但是,有些用户可能没有配置头像,所以Use
如何在 setTimeout 后调用带有 addEventListener 的函数?这是代码示例: xyz = setTimeout(function () { [...blabla func
我正在学习 Spring 并进行一些研发。 我尝试了“依赖”概念。 sparsh --> 25 3
我有一个 Rust 库,它基本上是一些 C 源代码和一个 bindgen 包装器。它有一个进入 C 源目录的 build.rs 脚本,运行 make,并将 .a 静态库复制到 $OUT_DIR。当我运
我使用 middleman用于开发网站并希望使用 .yml 文件将标记与内容分开。那么问题来了: 有没有办法根据某个 css 类更改 ruby 数据路径? 例如,我只想在父标签中有一个 css 类
此代码在转到 URL 的目标之前淡出每个页面。但是,在某些情况下,用户不会转到新页面,而是转到浏览器中的 PDF,或者打开默认邮件应用程序。在 Safari 上,如果您转到外部站点 (www.twit
在 Microsoft Solver Foundation 中,我想知道是否可以添加一个值取决于决策值的参数。 即我想要 TSP 模型的一些东西,但它还应该考虑从一个点到另一个点的流量。请注意:流量取
我正在尝试根据来自同一张表的 cid 获取新表中成人和 child 的总和。 餐 table 销售 +-----------+------------+---------------+ | cid
我有这个 .htaccess 文件,但我希望它只在我在实时站点上时才这样做。 有什么办法可以: // if server_name looks like example.com to this //
我对编程很陌生,现在一直在寻找解决方案,但由于我不确定我到底在寻找什么,所以我决定问你。 根据 Firebase 上的数据,我希望构建一个包含文本和图像的按钮。 例如,我有一个数据库,包括: User
我正在网上商店运行一个表单,如下所示: 按下提交按钮后,我根据从 $_POST 获得的信息在 php 中运行一个简单的错误检查。我有一个名为 $errors 的数组,如果我
我是一名优秀的程序员,十分优秀!