-
-
Save jccarbonfive/3774124 to your computer and use it in GitHub Desktop.
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
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| type User struct { | |
| name string | |
| } | |
| func (user User) String() string { | |
| return fmt.Sprintf("User: name = %s", user.name) | |
| } | |
| func main() { | |
| user := User{name: "foo"} | |
| fmt.Println(user) // User: name = foo | |
| } |
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
| package main | |
| import ( | |
| "fmt" | |
| "net/http" | |
| ) | |
| type User struct { | |
| name string | |
| } | |
| func (user User) String() string { | |
| return fmt.Sprintf("User: name = %s", user.name) | |
| } | |
| func (user User) ServeHTTP(writer http.ResponseWriter, request *http.Request) { | |
| fmt.Fprintf(writer, "hello %s", user.name) | |
| } | |
| func main() { | |
| user := User{name: "foo"} | |
| http.ListenAndServe("localhost:8888", user) // visit http://localhost:8888 | |
| // receive "hello foo" | |
| } |
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
| object App { | |
| case class Post(title: String, | |
| body: String, | |
| tags: List[String]) | |
| case class Address(street: String, | |
| city: String, | |
| state: String, | |
| zip: String, | |
| tags: List[String]) | |
| def formattedTags(taggable: { def tags: List[String] }) = { | |
| taggable.tags.mkString(",") | |
| } | |
| def main(args: Array[String]) { | |
| val post = Post("Weekly NFL Scores", | |
| "This week...", | |
| List("news", "sports", "local")) | |
| println(formattedTags(post)) // news,sports,local | |
| val address = Address("1 Main St.", | |
| "Anytown", | |
| "CA", | |
| "12345", | |
| List("home", "work")) | |
| println(formattedTags(address)) // home,work | |
| } | |
| } |
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
| # let user = | |
| object | |
| val name = "foo" | |
| val friends = [] | |
| method to_json = Printf.sprintf ("{ \"name\" : \"%s\" }") name | |
| method add_friend friend = friend :: friends | |
| end;; | |
| val user : < add_friend : 'a -> 'a list; to_json : string > = <obj> | |
| # let movie = | |
| object | |
| val title = "Predator" | |
| val rating = "R" | |
| method to_json = Printf.sprintf ("{ \"title\" : \"%s\" }") title | |
| method available user = user#age > 17 | |
| end;; | |
| val movie : < available : < age : int; .. > -> bool; to_json : string > = <obj> | |
| # let serialize jsonable = | |
| jsonable#to_json | |
| val serialize : < to_json : 'a; .. > -> 'a = <fun | |
| # serialize user;; | |
| - : string = "{ \"name\" : \"foo\" }" | |
| # serialize movie;; | |
| - : string = "{ \"title\" : \"Predator\" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment