This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fmt.Println(unsafe.Sizeof(filePerm)) // 1 | |
| var p Perm | |
| fmt.Println(unsafe.Sizeof(p)) // 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Perm struct { | |
| Read bool | |
| Write bool | |
| Execute bool | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| filePerm := Read | Write // set permission (| is bitwise OR) | |
| if filePerm&Write != 0 { // check permission (& is bitwise AND) | |
| fmt.Println("can write") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (p FilePerm) String() string { | |
| switch p { | |
| case Read: | |
| return "read" | |
| case Write: | |
| return "write" | |
| case Execute: | |
| return "execute" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type FilePerm uint8 | |
| const ( | |
| Read FilePerm = 1 << iota | |
| Write | |
| Execute | |
| ) | |
| func main() { | |
| fmt.Println(Execute) // execute |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In [5]: c._Client__address | |
| Out[5]: 'https://httpbin.org:443' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SocketClient(Client): | |
| def __init__(self, host, port): | |
| self.__address = (host, port) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In [4]: vars(c) | |
| Out[4]: {'_Client__address': 'https://httpbin.org:443'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ ipython | |
| In [1]: %run client.py | |
| In [2]: c = Client('httpbin.org', 443) | |
| In [3]: c.__address | |
| --------------------------------------------------------------------------- | |
| AttributeError Traceback (most recent call last) | |
| Input In [3], in <module> | |
| ----> 1 c.__address | |
| AttributeError: 'Client' object has no attribute '__address' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from urllib.request import URLError, urlopen | |
| from http import HTTPStatus | |
| class Client: | |
| def __init__(self, host, port): | |
| self.__address = f'https://{host}:{port}' | |
| def health(self): | |
| url = f'{self.__address}/health' |