### List SMB Share Names in Go Source: https://github.com/hirochachacha/go-smb2/blob/master/README.md This Go example demonstrates how to connect to an SMB server, authenticate using NTLM, and list all available share names. It establishes a TCP connection, dials an SMB2 session, and then calls `ListSharenames()` to retrieve the names. ```Go package main import ( "fmt" "net" "github.com/hirochachacha/go-smb2" ) func main() { conn, err := net.Dial("tcp", "SERVERNAME:445") if err != nil { panic(err) } defer conn.Close() d := &smb2.Dialer{ Initiator: &smb2.NTLMInitiator{ User: "USERNAME", Password: "PASSWORD", }, } s, err := d.Dial(conn) if err != nil { panic(err) } defer s.Logoff() names, err := s.ListSharenames() if err != nil { panic(err) } for _, name := range names { fmt.Println(name) } } ``` -------------------------------- ### Glob and Walk SMB Directory with Go's io/fs Interface Source: https://github.com/hirochachacha/go-smb2/blob/master/README.md This Go example demonstrates how to use the standard `io/fs` package functions, `iofs.Glob` and `iofs.WalkDir`, with an SMB file system. It connects to an SMB server, mounts a share, and then uses the `DirFS` method to expose the SMB share as an `io/fs.FS` interface for directory traversal and pattern matching. ```Go package main import ( "fmt" "net" iofs "io/fs" "github.com/hirochachacha/go-smb2" ) func main() { conn, err := net.Dial("tcp", "SERVERNAME:445") if err != nil { panic(err) } defer conn.Close() d := &smb2.Dialer{ Initiator: &smb2.NTLMInitiator{ User: "USERNAME", Password: "PASSWORD", }, } s, err := d.Dial(conn) if err != nil { panic(err) } defer s.Logoff() fs, err := s.Mount("SHARENAME") if err != nil { panic(err) } defer fs.Umount() matches, err := iofs.Glob(fs.DirFS("."), "*") if err != nil { panic(err) } for _, match := range matches { fmt.Println(match) } err = iofs.WalkDir(fs.DirFS("."), ".", func(path string, d iofs.DirEntry, err error) error { fmt.Println(path, d, err) return nil }) if err != nil { panic(err) } } ``` -------------------------------- ### Handle SMB Error Types in Go Source: https://github.com/hirochachacha/go-smb2/blob/master/README.md This Go example shows how to check for specific SMB error types using `os.IsNotExist`, `os.IsPermission`, and `os.IsTimeout`. It attempts to open a non-existent file, write to a read-only file, and perform an operation with a timeout, demonstrating how to identify common error conditions. ```Go package main import ( "context" "fmt" "net" "os" "github.com/hirochachacha/go-smb2" ) func main() { conn, err := net.Dial("tcp", "SERVERNAME:445") if err != nil { panic(err) } defer conn.Close() d := &smb2.Dialer{ Initiator: &smb2.NTLMInitiator{ User: "USERNAME", Password: "PASSWORD", }, } s, err := d.Dial(conn) if err != nil { panic(err) } defer s.Logoff() fs, err := s.Mount("SHARENAME") if err != nil { panic(err) } defer fs.Umount() _, err = fs.Open("notExist.txt") fmt.Println(os.IsNotExist(err)) // true fmt.Println(os.IsExist(err)) // false fs.WriteFile("hello2.txt", []byte("test"), 0444) err = fs.WriteFile("hello2.txt", []byte("test2"), 0444) fmt.Println(os.IsPermission(err)) // true ctx, cancel := context.WithTimeout(context.Background(), 0) defer cancel() _, err = fs.WithContext(ctx).Open("hello.txt") fmt.Println(os.IsTimeout(err)) // true } ``` -------------------------------- ### Perform SMB File Operations in Go Source: https://github.com/hirochachacha/go-smb2/blob/master/README.md This Go example illustrates how to perform basic file manipulation (create, write, read, remove) on an SMB share. It connects to the server, mounts a specified share, creates a file, writes content, seeks to the beginning, reads the content, and then removes the file. ```Go package main import ( "io" "io/ioutil" "net" "github.com/hirochachacha/go-smb2" ) func main() { conn, err := net.Dial("tcp", "SERVERNAME:445") if err != nil { panic(err) } defer conn.Close() d := &smb2.Dialer{ Initiator: &smb2.NTLMInitiator{ User: "USERNAME", Password: "PASSWORD", }, } s, err := d.Dial(conn) if err != nil { panic(err) } defer s.Logoff() fs, err := s.Mount("SHARENAME") if err != nil { panic(err) } defer fs.Umount() f, err := fs.Create("hello.txt") if err != nil { panic(err) } defer fs.Remove("hello.txt") defer f.Close() _, err = f.Write([]byte("Hello world!")) if err != nil { panic(err) } _, err = f.Seek(0, io.SeekStart) if err != nil { panic(err) } bs, err := ioutil.ReadAll(f) if err != nil { panic(err) } fmt.Println(string(bs)) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.