Interface in stdlib that returns []byte

blov · 2017-03-27 06:00:06 · 611 次点击    
这是一个分享于 2017-03-27 06:00:06 的资源,其中的信息可能已经有所发展或是发生改变。

Hi,

I am looking for a single method interface type in the stdlib with a method that just returns []byte. Any suggestions?


评论:

DenzelM:

What are you hoping to accomplish, exactly?

metakeule:

What are you hoping to accomplish with your question? :-D

DenzelM:

To understand your motivation, your intent. It's not exactly a question that I've ever come across, or a need. Mainly because there's usually a reason for conforming to an interface, i.e., not simply blind conformance. And I'm assuming, you have a reason... unless you don't. In other words, I'm looking for the root cause of your question. Make sense?

radovskyb:

Running an inspection library I wrote in Go, I have found at least these examples. I'm sure I could find more that return aliases for []byte if I checked, but here's a few to name some.

Package: httputil
Interface:  BufferPool
Method: func Get() []byte
Package: http
Interface:  http2headersOrContinuation
Method: func HeaderBlockFragment() []byte
Package: macho
Interface:  Load
Method: func Raw() []byte
Package: cipher
Interface:  AEAD
Method: func Seal(dst, nonce, plaintext, additionalData []byte) []byte
Package: tls
Interface:  macFunction
Method: func MAC(digestBuf, seq, header, data, extra []byte) []byte
Package: tls
Interface:  constantTimeHash
Method: func ConstantTimeSum(b []byte) []byte
Package: tls
Interface:  handshakeMessage
Method: func marshal() []byte
Package: hash
Interface:  Hash
Method: func Sum(b []byte) []byte

Edit: I've now included the package names for you to have a look if you'd like.

Edit 2: I actually just realized you asked for a single method interface, so the above probably are incorrect for that question.

Here are the single method ones from the above:

Package http:
Interface: http2headersOrContinuation
Method: func HeaderBlockFragment() []byte
Package tls:
Interface: constantTimeHash
Method: func ConstantTimeSum(b []byte) []byte
Package macho:
Interface: Load
Method: func Raw() []byte

Edit 3: The only single method interface out of the above that doesn't implement any other interfaces is the Load from the macho package one.

DenzelM:

That's pretty cool, is your tool open-source?

radovskyb:

Sure: https://github.com/radovskyb/inspect.

I just modified the example here to get the results: https://github.com/radovskyb/inspect/tree/master/example/extract_all_interfaces.

Now if only I could remember how to format better on reddit xD

Edit: The tool is quite simple at the moment and haven't really worked on it in a while, but I'm sure I'll slowly add to it over time like I have been once in a great while :P

DenzelM:

That's sweet, thanks for sharing! It'd be awesome to turn it into a cmdline tool that could construct queries that match patterns. Then we could run these type of queries as a one-liner; making Inspect a great compliment to Go Guru.

I'm not suggesting you do this, per se, since it would be a lot of work and I'm sure you have your hands full. I'm just thinking out loud.

radovskyb:

I'm not the busiest person lately so that isn't much of a problem :P

If I or someone else was going to turn it into a cmd line tool rather than just a library, I might consider first rewriting some of it to make it a bit better (it's not too bad at the moment I suppose), but then again, could be a fun idea :P

metakeule:

I hoped for a more general one like fmt.Stringer. debug/macho looks like a very specialized lib, seeing it for the first time.. ;-) But thanks anyway.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

611 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传