dongmi4035 2017-03-01 08:41
浏览 5
已采纳

完成处理包后,强制调用Close()

I'm writing a package in go that creates a connection to Kafka.

I create the connection in the init() function in the main file in the package. After the program that uses the package stops, I want to call a function called Close()

Is there a way to enforce it in the package level, instead of giving this responsibility to the user?

Assume that the connection should be available throughout the run of the user's program, I don't want to initialize it every time.

  • 写回答

1条回答 默认 最新

  • doswy02440 2017-03-01 10:51
    关注

    As for just about any resource (and a connection is no different), a user should call the functions to return resources to the system after use. Most users understand this across languages and architectures, so I don't see why your package users should have any concern. Go addresses this problem very elegantly by including the defer statement which makes it very easy to provide for releasing a resource.

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

报告相同问题?

悬赏问题

  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问