File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "os"
6
+ )
7
+
8
+ func main () {
9
+ // Declare variable of type os.File and Create file test.txt
10
+ file , err := os .Create ("test.txt" )
11
+ //handle checking error
12
+ if err != nil {
13
+ //display if having error
14
+ fmt .Println ("Error creating file : " , err )
15
+ }
16
+ // closing file after use
17
+ defer file .Close ()
18
+
19
+ // write to file
20
+ _ , err = file .WriteString ("Testing I can write to this file" )
21
+ //handle checking error
22
+ if err != nil {
23
+ fmt .Println ("Error writing to file : " , err )
24
+ return
25
+ }
26
+
27
+ //define permissions
28
+ err = os .Chmod ("test.txt" , 0644 )
29
+ //handle checking error
30
+ if err != nil {
31
+ fmt .Println ("Error changing file permissions :" , err )
32
+ return
33
+ }
34
+
35
+ // When everything is ok
36
+ fmt .Println ("File created and permissions successfully" )
37
+ }
You can’t perform that action at this time.
0 commit comments