aboutsummaryrefslogtreecommitdiff
path: root/src/os/example_test.go
AgeCommit message (Collapse)Author
2024-07-24os: document CopyFS behavior for symlinks in destinationDamien Neil
Also clarify the permissions of created files, and note that CopyFS will not overwrite files. Update a few places in documentation to use 0oXXX for octal consts. For #62484 Change-Id: I208ed2bde250304bc7fac2b93963ba57037e791e Reviewed-on: https://go-review.googlesource.com/c/go/+/600775 Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-01-10os: relax tests and add examples for UserCacheDir and UserConfigDirBryan C. Mills
Per https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html: “If, when attempting to write a file, the destination directory is non-existent an attempt should be made to create it with permission 0700. […] The application should be prepared to handle the case where the file could not be written […]. In such case it may choose to present an error message to the user.” In certain CI environments, these directories have well-defined locations but do not exist and cannot be created. In that case, we now choose to log and return from the test without failing it. To prevent the functions from falling back to being entirely untested, we still fail the test (and “present an error message to the user”) if either function returns an empty string without an error, or returns a path that refers to a non-directory or results in an error other than ErrNotExist. In addition, since the tests themselves no longer create subdirectories, we add examples illustrating the suggested pattern of usage. Fixes #64990. Change-Id: Ie72106424f5ebe36eaf9288c22710d74bb14a462 Reviewed-on: https://go-review.googlesource.com/c/go/+/554815 Reviewed-by: Austin Clements <austin@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com>
2023-12-07os: document Readlink behavior for relative linksBryan C. Mills
Also provide a runnable example to illustrate that behavior. This should help users to avoid the common mistake of expecting os.Readlink to return an absolute path. Fixes #57766. Change-Id: I8f60aa111ebda0cae985758615019aaf26d5cb41 Reviewed-on: https://go-review.googlesource.com/c/go/+/546995 Auto-Submit: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2023-07-20os: remove executable bits from os.OpenFile exampleRami
The mode used in the os.OpenFile example (0755) has all executable bits set. I suspect that this ill-advised usage propagates to other codebases (by means of people carelessly copying the usage example), which is why I suggest modifying the example. Change-Id: Ic36c8b41974f3fe00471822c2414e36b4e5dc1bc GitHub-Last-Rev: 638f3beefe8926c8e5c2c4ab9a1a5899e55de892 GitHub-Pull-Request: golang/go#61384 Reviewed-on: https://go-review.googlesource.com/c/go/+/510135 Reviewed-by: Rob Pike <r@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com> Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-03-29os: don't check for IsExist in MkdirAll exampleStefan Dombrowski
If a directory already exists, then MkdirAll returns nil. Therefore the check with IsExist is not necessary. Change-Id: Idf83c056f64bb56f49eb2b649af7827b759bcd7c GitHub-Last-Rev: 1f29873d0cd852642938cbd899549b9bf04301da GitHub-Pull-Request: golang/go#53242 Reviewed-on: https://go-review.googlesource.com/c/go/+/410434 Run-TryBot: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-02-08os: add examples for Mkdir and MkdirAllAdrian Hesketh
Provides example using value for the perm argument that matches the value set by the mkdir command on MacOS and Linux. Change-Id: I98d9ac9668de4dc0efde2484f5b00d005628ac9e GitHub-Last-Rev: 44e617912f3604f4cc05a946d76cd3020acfd722 GitHub-Pull-Request: golang/go#50641 Reviewed-on: https://go-review.googlesource.com/c/go/+/378874 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Cherry Mui <cherryyz@google.com>
2021-06-30os: change example to avoid deprecated functionEli Bendersky
The IsNotExist function is deprecated; change package example to avoid it and use the recommended way instead. Fixes #46976 Change-Id: I3c301d0a89b6bda42184df314ba8418062ca39ee Reviewed-on: https://go-review.googlesource.com/c/go/+/331692 Trust: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2020-12-02os: add ReadFile, WriteFile, CreateTemp (was TempFile), MkdirTemp (was ↵Russ Cox
TempDir) from io/ioutil io/ioutil was a poorly defined collection of helpers. Proposal #40025 moved out the generic I/O helpers to io. This CL for proposal #42026 moves the OS-specific helpers to os, making the entire io/ioutil package deprecated. For #42026. Change-Id: I018bcb2115ef2ff1bc7ca36a9247eda429af21ad Reviewed-on: https://go-review.googlesource.com/c/go/+/266364 Trust: Russ Cox <rsc@golang.org> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
2020-10-20all: update references to symbols moved from os to io/fsRuss Cox
The old os references are still valid, but update our code to reflect best practices and get used to the new locations. Code compiled with the bootstrap toolchain (cmd/asm, cmd/dist, cmd/compile, debug/elf) must remain Go 1.4-compatible and is excluded. For #41190. Change-Id: I8f9526977867c10a221e2f392f78d7dec073f1bd Reviewed-on: https://go-review.googlesource.com/c/go/+/243907 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2019-04-11os: don't leak file in ExampleOpenFile_append failure pathjames
Fixes #31424 Change-Id: I8364578cbc77827552bd764c716f68495ec51547 Reviewed-on: https://go-review.googlesource.com/c/go/+/171763 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-12-18os: show how to print permission bits in octalKevin Burke
Permission bits are most commonly viewed in string form (rwx-- etc) or in octal form (0755), but the latter is relatively rare in Go. Demonstrate how to print a FileMode in readable octal format. Change-Id: I41feb801bcecb5077d4eabafdea27c149fc179a1 Reviewed-on: https://go-review.googlesource.com/c/154423 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-10-03os: add UserHomeDirBrad Fitzpatrick
Fixes #26463 Change-Id: Iaef1c7456ffaeadeead6027a37d09c44a3d05bd5 Reviewed-on: https://go-review.googlesource.com/c/139418 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-08-24os: use Println instead of Printf in exampleSeebs
This message has no format specifiers and no trailing newline. It should use Println for consistency with other examples. Change-Id: I49bd1652f9449fcbdd79c6b689c123090972aab3 Reviewed-on: https://go-review.googlesource.com/127836 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-05-23os: Add example for Expand function.azat
Change-Id: I581492c29158e57ca2f98b75f47870791965a7ff Reviewed-on: https://go-review.googlesource.com/81155 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-07-15os: use Fstat instead of Stat in FileMode exampleTaro Aoki
The existing example for FileMode using Stat to get FileInfo. But, Stat cannot get symlink info, it need to use Fstat instead. Change-Id: I5cc38cd10caaa5912946abe2a2b90995a91ee10f Reviewed-on: https://go-review.googlesource.com/47370 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2017-03-01os: add OpenFile example for appending dataKevin Burke
Fixes #19329. Change-Id: I6d8bb112a56d751a6d3ea9bd6021803cb9f59234 Reviewed-on: https://go-review.googlesource.com/37619 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-10-12os: add examples of environment functionsJean-Nicolas Moal
For #16360. Change-Id: Iaa3548704786018eacec530f7a907b976fa532fe Reviewed-on: https://go-review.googlesource.com/27443 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-10-12os: add example for IsNotExistSina Siadat
Show usage of os.IsNotExist in an example. Change-Id: I5306ea06c370099de5b02668dfa02b87b0c2beac Reviewed-on: https://go-review.googlesource.com/25571 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-09-14os: add more examplesEmmanuel Odeke
Updates #16360. Adds examples for: + Chmod + Chtimes + FileMode Change-Id: I1b61ee0392fa3774593a7f36aaf0fa1e484c778b Reviewed-on: https://go-review.googlesource.com/28963 Run-TryBot: Andrew Gerrand <adg@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-08-29os: add example for OpenFileJaana Burcu Dogan
New beginners are not familiar with open(2)-style masking of the flags. Add an example demonstrates the flag or'ing. Change-Id: Ifa8009c55173ba0dc6642c1d3b3124c766b1ebbb Reviewed-on: https://go-review.googlesource.com/27996 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>