class File:
name: str
context: str
file: open
def __init__(self, name):
self.name = name
def open(self):
self.file = open(self.name)
def read(self):
return self.file.read()
def write(self, text):
self.file.write(text)
def close(self):
self.file.close()
def __del__(self):
self.file.close()
1 go 没有构造函数和析构函数,
__init__ 和 __del__ 需要你写在自己的逻辑里面
2 file 用标准库 os.File 代替即可
3 open/read/write 用标准库文件操作对应实现即可
#4
更多评论
class File:
name: str
context: str
file: open
def __init__(self, name):
self.name = name
def open(self):
self.file = open(self.name)
def read(self):
return self.file.read()
def write(self, text):
self.file.write(text)
def close(self):
self.file.close()
def __del__(self):
self.file.close()
#1
package main
type F interface {
read() string
write() string
open()
close()
}
type f struct {
name string
err error
}
func main() {
}
#2