以下内容转载自 https://stackoverflow.com/questions/60518823/error-cannot-use-promoted-field-in-struct-literal-of-type

This question already has an answer here:

How do I initialize a composed struct in Go? (1 answer)

Closed 8 months ago.

I have two packages where Package B imports Package A, like this:

Package A

package Atype Car struct {Color       stringMake        stringModel       string
}

Package B

package Btype car struct {*A.Car
}func NewCar() car {return &car{Color: "red",Make:  "toyota",Model: "prius"}
}

However, this gives me the error: cannot use promoted field Car.Color in struct literal of type car inside NewCar function, how do I fix this? Everything I have read online just makes me more confused.

go

share  improve this question  follow

edited Mar 4 at 8:08

Flimzy

56.2k1313 gold badges8787 silver badges128128 bronze badges

asked Mar 4 at 3:51

JAM

46488 silver badges1818 bronze badges

  • 2

    Does How do I initialize a composed struct in Go? or nested struct initialization literals answer your question? – Muffin Top Mar 4 at 4:03

add a comment

1 Answer

ActiveOldestVotes

10

You need:

func NewCar() *car {return &car{ &A.Car{Color: "red",Make:  "toyota",Model: "prius",}}
}

See https://github.com/golang/go/issues/9859 for:

proposal: spec: direct reference to embedded fields in struct literals

error: cannot use promoted field in struct literal of type [duplicate]相关推荐

  1. C++报错解决:error: ‘int’ is not a class, struct, or union type typedef typename _Sequence::value_

    我在使用C++的时候,遇到了如下报错: -> % g++ test.cpp In file included from /usr/include/c++/7/queue:64:0,from te ...

  2. Golang postgres.go:45:3: unknown field ‘WithReturning‘ in struct literal of type....

    异常原因:该依赖版本过低 解决:更新依赖 go get -u gorm.io/driver/postgres

  3. ERROR 1062 (23000): ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '1'

    测试环境 1.MYSQL版本 +------------+ | version() | +------------+ | 5.7.18-log | +------------+2.建表语句 mysql ...

  4. MapReduce操作时Error:The method setInputPaths(JobConf, String) in the type FileInputFormat is not

    问题描述 今天在写java API的MapReduce操作时,出现了Error:The method setInputPaths(JobConf, String) in the type FileIn ...

  5. Type string trivially inferred from a string literal, remove type annotation.eslint@typescript-eslin

    TypeScript代码类似: const web: string = "Hello World"; 如果是用eslint 验证会导致报错 Type string triviall ...

  6. Type string trivially inferred from a string literal, remove type annotation

    Angular报错:Type string trivially inferred from a string literal, remove type annotation stockCode: st ...

  7. ROOT/RStringView.hxx:32:37: error: ‘experimental’ in namespace ‘std’ does not name a type

    问题 在包含ROOT代码的Geant4文件编译时,可能会报错: ROOT/RStringView.hxx:32:37: error: 'experimental' in namespace 'std' ...

  8. error[E0277]: Rust语言如何print struct内容?

    直接使用println!宏会报错 struct Coordinate {x: u32,y: u32, }fn main() {let coor = Coordinate {x: 1,y: 5,};pr ...

  9. Field error in object ** on field **

    错误信息: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindi ...

最新文章

  1. centos7 安装 xfce桌面
  2. Soul网关发布里程碑的2.3.0版本,新增支持GRPC,Tars,Sofa协议
  3. 那年高考“坑爹“的金思力
  4. P3239 [HNOI2015]亚瑟王(期望)
  5. 【安卓开发 】Android初级开发(六)Activity生命周期
  6. ubuntu16.04caffe训练mnist数据集
  7. LeetCode题库5:最长回文子串——JavaScript解答
  8. camera (19)---Android 相机开发的基本流程
  9. SQL 2012企业版安装教程
  10. 影视剪辑,PR剪辑软件两个转场教程
  11. 国际电话登机英文术语
  12. php企鹅号_腾讯内容开放平台
  13. Arduino之读取温度传感器并串口显示
  14. 【紧急情况】:回宿舍放下书包的我,花了20分钟敲了一个抢购脚本
  15. SSM框架实现数据库存储图片及读取图片
  16. Matlab R2012a/b反复激活无效+license checkout failed解决方案
  17. 1 error LNK2019: 无法解析的外部符号 _Direct3DCreate9@4,该符号在函数 void __cdecl InitD3D(struct HWND__ *)
  18. 计算机乘法算法流程,布斯乘法算法
  19. Java-根据IP统计访问次数
  20. 对话本体创始人李俊——有关IEO、DApp、区块链游戏的一些冷思考

热门文章

  1. ubuntu下安装libtorch
  2. 计算机我的手机选项在哪里设置方法,手把手教你用手机轻松设置无线wifi路由器的方法...
  3. php 对图片缩放,php 图片等比例缩放和截图
  4. Java并发编程 - 共享模型之管程
  5. Ubuntu16.04笔记本 安装R RStudio
  6. 黑马程序员—对话框Dialog小例子
  7. 使用Docker快速安装部署ES和Kibana并配置IK中文分词器以及自定义分词拓展词库
  8. 一、jSP简介(前置知识)
  9. 国内常用的ntp服务器
  10. python编程入门书-最适合Python初学者的6本书籍推荐「必须收藏」