Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions _demo/go/oswritestring/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"os"
)

func main() {
f, err := os.CreateTemp("", "llgo-writestring-*.txt")
if err != nil {
panic(err)
}
Comment on lines +9 to +12

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While using panic is acceptable for a simple demo, it's generally better to use log.Fatalf for handling expected errors like I/O failures. This provides a cleaner error message without a full stack trace, which is more idiomatic for command-line applications and tests.

I'd recommend replacing all panic calls in this file with log.Fatalf. For example, this block could be changed to:

	if err != nil {
		log.Fatalf("Failed to create temp file: %v", err)
	}

This would require adding "log" to the imports.

defer os.Remove(f.Name())

const content = "hello writestring"
if n, err := f.WriteString(content); err != nil {
panic(err)
} else if n != len(content) {
panic(fmt.Sprintf("WriteString wrote %d bytes, want %d", n, len(content)))
}

if err := f.Close(); err != nil {
panic(err)
}

data, err := os.ReadFile(f.Name())
if err != nil {
panic(err)
}
if string(data) != content {
panic(fmt.Sprintf("content mismatch: got %q, want %q", string(data), content))
}

fmt.Println("ok")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The demo validates the happy path well. For more robust testing, consider adding cases for:

  • Empty string: f.WriteString("")
  • Multiple consecutive writes to verify behavior
  • Large strings (e.g., strings.Repeat("x", 10*1024*1024))

This would provide better confidence in edge case handling, especially since this enables previously disabled functionality.

8 changes: 3 additions & 5 deletions runtime/internal/lib/os/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"syscall"
"time"
"unsafe"
)

// Name returns the name of the file as presented to Open.
Expand Down Expand Up @@ -201,11 +202,8 @@ func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
// WriteString is like Write, but writes the contents of string s rather than
// a slice of bytes.
func (f *File) WriteString(s string) (n int, err error) {
/*
b := unsafe.Slice(unsafe.StringData(s), len(s))
return f.Write(b)
*/
panic("todo: os.(*File).WriteString")
b := unsafe.Slice(unsafe.StringData(s), len(s))
return f.Write(b)
}

// Open opens the named file for reading. If successful, methods on
Expand Down
Loading