M
Modular7mo ago
toasty

Gojo: Experiments in porting over Golang stdlib into Mojo

https://github.com/thatstoasty/gojo/tree/main This library is my attempt at porting over some modules and structs from Golang's stdlib. You can find the list below! Most of these topics were new to me when starting the port, so if you find bugs or opportunities for improvement feel free to drop some comments! It's been a good learning opportunity for both Go and Mojo. Modules - bufio - Reader: Buffered io.Reader - Scanner: Scanner interface to read data via tokens. - bytes - Buffer: Buffer backed by UnsafePointer[UInt8]. - Reader: Reader backed by UnsafePointer[UInt8]. - io - Traits: Reader, Writer, Seeker, Closer, ReadWriter, ReadCloser, WriteCloser, ReadWriteCloser, ReadSeeker, ReadSeekCloser, WriteSeeker, ReadWriteSeeker, ReaderFrom, WriterReadFrom, WriterTo, ReaderWriteTo, ReaderAt, WriterAt, ByteReader, ByteScanner, ByteWriter, StringWriter - Reader and Writer wrapper functions. - FileWrapper: FileHandle Wrapper Reader/Writer - STDOUT/STDERR Writer (leveraging libc). - strings - StringBuilder: String builder for fast string concatenation. - Reader: String reader. - fmt - Basic sprintf function. - syscall - External call wrappers for libc functions and types. - net - Socket: Wraps FileDescriptor and implements network specific functions. - FileDescriptor: File Descriptor wrapper that implements io.Writer, io.Reader, and io.Closer. - Dial and Listen interfaces for Socket connections (for TCP only atm). - Socket struct similar to Python's and a simple FileDescriptor struct.
2 Replies
toasty
toastyOP7mo ago
Gojo has been updated to include some UDP socket structs! I've also included some examples for some basic tcp and udp server/clients and an example of using bufio.Scanner. https://github.com/thatstoasty/gojo/tree/main/examples Example UDP server using the listen_udp function
from gojo.net import UDPAddr, listen_udp, HostPort
import gojo.io


fn main() raises:
var listener = listen_udp("udp", UDPAddr("127.0.0.1", 12000))

while True:
var dest = List[UInt8](capacity=16)
var bytes_read: Int
var remote: HostPort
var err: Error
bytes_read, remote, err = listener.read_from(dest)
if err:
raise err

dest.append(0)
var message = String(dest^)
print("Message received:", message)
message = message.upper()
var bytes_sent: Int
bytes_sent, err = listener.write_to(message.as_bytes(), UDPAddr(remote.host, remote.port))
print("Message sent:", message)
from gojo.net import UDPAddr, listen_udp, HostPort
import gojo.io


fn main() raises:
var listener = listen_udp("udp", UDPAddr("127.0.0.1", 12000))

while True:
var dest = List[UInt8](capacity=16)
var bytes_read: Int
var remote: HostPort
var err: Error
bytes_read, remote, err = listener.read_from(dest)
if err:
raise err

dest.append(0)
var message = String(dest^)
print("Message received:", message)
message = message.upper()
var bytes_sent: Int
bytes_sent, err = listener.write_to(message.as_bytes(), UDPAddr(remote.host, remote.port))
print("Message sent:", message)
aurelian
aurelian6mo ago
@toasty nice! looking forward to unix sockets
Want results from more Discord servers?
Add your server