Reusable code snippets with syntax highlighting and copy support.
1# Write to file
2with open("file.txt", "w") as file:
3 file.write("Hello World")
4
5# Read file
6with open("file.txt", "r") as file:
7 content = file.read()
8 print(content)1async function getUsers() {
2 try {
3 const response = await fetch("https://jsonplaceholder.typicode.com/users");
4 const data = await response.json();
5
6 console.log("Users:", data);
7 } catch (error) {
8 console.error("Error fetching users:", error);
9 }
10}
11
12getUsers();1interface ApiService {
2 @GET("users")
3 suspend fun getUsers(): List<User>
4}
5
6object RetrofitInstance {
7 val api: ApiService by lazy {
8 Retrofit.Builder()
9 .baseUrl("https://api.example.com/")
10 .addConverterFactory(GsonConverterFactory.create())
11 .build()
12 .create(ApiService::class.java)
13 }
14}1import kotlinx.coroutines.*
2
3fun loadUserData() {
4 CoroutineScope(Dispatchers.IO).launch {
5 val data = fetchFromServer()
6
7 withContext(Dispatchers.Main) {
8 println("User Data: $data")
9 }
10 }
11}
12
13suspend fun fetchFromServer(): String {
14 delay(1000)
15 return "User Loaded Successfully"
16}