dongye9228 2016-12-14 02:33
浏览 262
已采纳

如何在go中使用os.Name()?

I'm trying to use: https://golang.org/src/os/file.go?s=1472:1577#L35

by importing the "os" package and then running

f, err := os.Open("/tmp/dat3")
check(err)
name := os.Name(f)

I get ./main.go:29: undefined: os.Name

Why? What am I doing wrong.

(Of course I know I have the name of the open file - but I'm curious why, I'm not able to call that function)

  • 写回答

1条回答 默认 最新

  • doutang7415 2016-12-14 02:58
    关注

    Because Name is a special function (method) defined on File struct. Means it takes File type as receiver and can be called using receiver instance (in your case f).

    This should work

    name := f.Name()

    Read more at :

    https://tour.golang.org/methods/1

    https://gobyexample.com/methods

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘